Today lets see how we can track the index number of the loop. i.e., the number of completed iterations in a loop. We shall also learn *ngIf conditional operator and its usage.
ngIf is called conditional operator because it operates based on conditions i.e., if the conditional statement it has been assigned is true, then the node it is attached to will be rendered or else it’ll not. *ngIf takes boolean values.
Example: If *ngIf is attached to a div and the condition is false, then the div it has been attached to won’t be added to the DOM. If the condition is true, then div is added to the DOM.
index, first, last While we are looping through some array elements inside template file we can know the index of the loop using index variable. We can use ngIf and do something at the first iteration using first variable and similarly we can know the last iteration using last variable.
<ion-list no-lines>
<ion-item *ngFor="let company of companies;
let i = index;
let lst = last;
let fst = first;">
{{i+1}}. {{company.name}}
<span *ngIf="lst"> - Am last!</span>
<span *ngIf="fst"> - Am first</span>
</ion-item>
</ion-list>
inside *ngFor we have initiated and assigned index value to variable i, last index value to variable lst and first index value to variable fst.
Inside ion-item we are using *ngIf to check if the iteration is first iteration, if so add ‘Am first’ message besides the first list item i.e., beside Microsoft. We also check if the iteration is a last iteration, if so we display ‘Am last!’ message beside the last item in the list i.e., beside IBM.
In this example, we take some list of images and using CSS we make the display property to none. Hence hiding all the images from being displayed. Using filter methods we write our jQuery script to fetch elements(images).
Here, we select ol tag and fetch all its children, and inturn select its 4th child using its index number, to which we apply the css property of display to inline.
Here, we select ol tag and fetch all its children, and inturn select its children from index 1 to 4 i.e., image 1, 2 and 3, to which we apply the css property of display to inline.
Here, we select ol tag and fetch all its children, and inturn select its children which do not have the class name no i.e., image 0, 1 and 3, to which we apply the css property of display to inline.
This is opposite of not() method. Here, we select ol tag and fetch all its children, and inturn select its children which has the class name no i.e., image 2 and 4, to which we apply the css property of display to inline.
Video Tutorial: jQuery Filter Methods To Narrow Selection
These are the 6 filter methods of jQuery, using which we can narrow down our selection.
I’m using images to make sure the learning stays colorful and not pale boring text. These filter methods will come in handy in later stages wherein we under take some complex application developments. These type of methods save us time and the number of line of code, to achieve the same thing manually.