menu
AngularJS Type Properties Guide
AngularJS Type Properties Guide
ng-app directive defines an AngularJS application. ng-controller directive defines an application controller. The novalidate attribute is not required in your application, but you need to use it in AngularJS forms to override standard HTML5 validation.

Primarily each and every single web application getting developed around gets inputs from its users. Maybe it’s got a comment feed having a handful of text boxes. Or perhaps it has some kind of calculator with various inputs and sliders. Naturally, there’s pretty much generally the login page. Yes, the e mail and password are inputs too. Get more information about AngularJS Forms

When working on web apps you’re going to become handling inputs rather a little, and in that case, you should be well equipped to work with the proper tools for the job. With AngularJS, these tools should really include the extensive support for types, inputs and validations.

I’ve covered the fundamentals of writing types before, but within this short article I’d like to point out how Angular’s forms possess a few magic properties which can be worth recognizing, considering the fact that they're able to spare you some bugs and code!

Initially Things First: Having Access for the Type

Types in AngularJS have special properties, but how specifically are you currently meant to acquire access to these types? The trick is to name the form. After supply a name for the types, AngularJS will automatically expose it under that name within your scope.

For example, say that we've this as a part of the template of a component with $ctrl as its controller-as name:

<form name="$ctrl.exampleForm">

 <!-- inputs etc. go here.. -->

</form>

Setting the name to $ctrl.exampleForm implies that in the template we can get access to the kind, simply by using $ctrl.exampleForm. It might also be accessed from the controller’s code, using this.exampleForm.

Now that we understand how to obtain access towards the kind, let’s start creating use of it!

Testing Whether or not the User Has Interacted Together with the Kind

An extremely prevalent use case will be the need to display particular error messages or help tips only immediately after the user has began changing values in the type (or hasn’t started however).

To do just that, forms in AngularJS come supplied with two handy boolean properties, $pristine and $dirty. These two booleans are normally the damaging of your other (i.e. $pristine === !$dirty).

When the form is in its virgin state as well as the user hasn’t changed anything yet, $pristine is set to true. When the user has interacted together with the form’s inputs, $pristine is set to false and $dirty is true.

In case you need to programmatically force the kind back to its pristine state (e.g. the user clicked on reset, or after a effective save), it is possible to contact $ctrl.exampleForm.$setPristine().

Display Points Immediately after Kind Submission

In some cases, we want kind validations to only be displayed right after the user has clicked the save button, as an alternative to changing because the user sorts or moves amongst fields.

In those cases, basically hiding validations till the type becomes $dirty won’t do, that is precisely why forms also have the handy $submitted property. This property gets set to true after the user has submitted the type, even though the kind is invalid.

Submitting a kind signifies clicking a button which has the attribute type="submit", or pressing Enter/Return inside an input.

AngularJS will not avoid the form from being submitted if it’s invalid, meaning your ng-submit callback is named. You'll need to create sure not to act in case the kind is not within a valid state.

Checking if the Kind Is Valid

And just as a way to verify whether or not the kind is valid or not, types come equipped with a handful of a lot more swanky properties.

Very first of all will be the $valid and $invalid couple. If $valid is true - go ideal ahead. If $invalid is true - something is amiss.

In case the form is invalid, the form’s $error hash will contain all of the needed details about which fields are invalid and for what validations.

But, there’s a different state right here, that is when both are undefined. That is probable when the type has asynchronous validators. This signifies that it’s essential to test they are true or false and not only “falsy” (e.g. undefined or null).

You may also verify irrespective of whether the kind is currently pending, and see which on the validators are becoming processed, by accessing the $pending hash (which can be structured similarly to $error).

There’s lots more that could be written about types and their inputs in AngularJS, so if you’d like to hear additional please subscribe beneath!