16.07.2017 Views

AngularJS Essentials

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3<br />

Understanding the $pristine and $dirty<br />

properties<br />

Sometimes, it would be useful to know whether the field was never touched in order<br />

to trigger (or not) some validation processes. This can be done by the means of two<br />

objects with very suggestive names: $pristine and $dirty.<br />

Pristine means purity, and here, it denotes that the field wasn't touched by anyone.<br />

After it's been touched for the first time, it becomes dirty. So, the value of $pristine<br />

always starts with true and becomes false after any value is typed. Even if the field<br />

is empty again, the value remains false. The behavior of the $dirty object is just the<br />

opposite. It is by default false and becomes true after the first value is typed:<br />

<br />

The plate is invalid!<br />

<br />

The $error object<br />

In the end, the one that remains is the $error object. It accumulates the detailed list<br />

of everything that happens with the form and can be used to discover which field<br />

must be proofread in order to put the form in a valid situation.<br />

Let's use it to help our users understand what's exactly going wrong with the form:<br />

<br />

<br />

You must inform the plate of the car!<br />

<br />

<br />

The plate must have at least 6 characters!<br />

<br />

<br />

The plate must have at most 10 characters!<br />

<br />

[ 65 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!