AngularJS Kind Properties Guide

Posted by Thomas Shaw on December 29th, 2019


Primarily just about every single web application getting developed on the market gets inputs from its users. Possibly it is got a comment feed having a few text boxes. Or maybe it has some sort of calculator with diverse inputs and sliders. Of course, there’s practically constantly the login page. Yes, the e-mail and password are inputs as well. Get additional information and facts about AngularJS Forms



When operating on web apps you’re going to become handling inputs pretty a little, and if that's the case, you should be effectively equipped to make use of the ideal tools for the job. With AngularJS, these tools really should incorporate the comprehensive support for types, inputs and validations.

I’ve covered the fundamentals of writing types before, but within this article I’d prefer to point out how Angular’s types possess a handful of magic properties that are worth recognizing, due to the fact they're able to spare you some bugs and code!

Initially Items First: Acquiring Access to the Type
Forms in AngularJS have particular properties, but how exactly are you currently meant to get access to these forms? The trick would be to name the type. Once give a name for the forms, AngularJS will automatically expose it below that name in your scope.

As an example, say that we have this as 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 inside the template we can get access towards the type, simply by using $ctrl.exampleForm. It might also be accessed from the controller’s code, using this.exampleForm.

Now that we know how to get access to the kind, let’s start off making use of it!

Testing Regardless of whether the User Has Interacted Together with the Type
An incredibly typical use case may be the have to have to display certain error messages or assist recommendations only following the user has started altering values inside the type (or hasn’t began yet).

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

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

In case you'll need to programmatically force the kind back to its pristine state (e.g. the user clicked on reset, or just after a profitable save), you are able to call $ctrl.exampleForm.$setPristine().

Display Issues Immediately after Type Submission
In some cases, we want form validations to only be displayed just after the user has clicked the save button, rather than changing as the user sorts or moves between fields.

In those cases, merely hiding validations till the type becomes $dirty will not do, which can be precisely why types also have the handy $submitted property. This property gets set to true as soon as the user has submitted the form, even when the form is invalid.

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

AngularJS won’t protect against the type from being submitted if it’s invalid, meaning your ng-submit callback is called. You need to make certain to not act in case the kind is not within a valid state.

Checking if the Type Is Valid
And just in an effort to verify no matter whether the type is valid or not, forms come equipped having a few additional swanky properties.

1st of all would be the $valid and $invalid couple. If $valid is true - go suitable ahead. If $invalid is true - one thing is amiss.

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

But, there’s a further state here, which is when each are undefined. This really is achievable when the form has asynchronous validators. This implies that it is critical to test these are true or false and not just “falsy” (e.g. undefined or null).

You may also check no matter if the type is currently pending, and see which on the validators are being processed, by accessing the $pending hash (which can be structured similarly to $error).

There’s lots additional which will be written about types and their inputs in AngularJS, so if you’d like to hear much more please subscribe under!

Like it? Share it!


Thomas Shaw

About the Author

Thomas Shaw
Joined: March 17th, 2018
Articles Posted: 11,324

More by this author