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.
Creating Reusable Components with Directives<br />
Beyond iterating, we might need to identify which is the first or the last element,<br />
what is its index number, and many other things. This can be achieved by using the<br />
following properties:<br />
Variable Type Details<br />
$index number Number of the element<br />
$first Boolean This is true if the element is the first one<br />
$last Boolean This is true if the element is the last one<br />
$middle Boolean This is true if the element is in the middle<br />
$even Boolean This is true if the element is even<br />
$odd Boolean This is true if the element is odd<br />
The ngModel directive<br />
The ngModel directive attaches the element to a property in the scope, thus binding<br />
the view to the model. In this case, the element can be input (all types), select,<br />
or textarea, as shown in the following code:<br />
<br />
There is an important piece of advice regarding the use of this directive. We must<br />
pay attention to the purpose of the field that is using the ngModel directive. Every<br />
time the field is a part of the construction of an object, we must declare the object<br />
in which the property should be attached. In this case, the object that is being<br />
constructed is a car; so, we will use car.plate inside the directive expression.<br />
However, sometimes it may so happen that there is an input field that is just used to<br />
change a flag, allowing the control of the state of a dialog or another UI component.<br />
In this case, we can use the ngModel directive without any object as long as it will not<br />
be used together with other properties or even persisted.<br />
In Chapter 5, Scope, we will go through the two-way data binding concept. It is very<br />
important to understand how the ngModel directive works behind the scenes.<br />
[ 24 ]<br />
www.it-ebooks.info