Use for loop in the angular

Looping through Arrays in ES5 javascript.

Simple for

let array = [1,2,3];
for (let i =0; i <array.length;i++){
console.log(array[i];
}

for-each:

let array=[1,2,3];
array.forEach(function(value){
console.log(value);
}

for-in:

let array=[1,2,3];
for(let a in array){
console.log(a):
}

for-of:

let array=[1,2,3];
for(var a in array){
console.log(a)
}

ng-repeat vs *ngFor

ng-repeat available in the angular1.X 
and *ngFor is available in the angular 2 or more

  1. ng-repeat directive instantiated template once per item for a collection.
  2. Each template instance had its own scope.
  3. Special properties were available for each template instance: $index$first$middle$last$even$odd.
  4. ng-repeat by default did not allow duplicate elements. A tracking function was responsible for this task.
  5. In order to add duplicate items, track by expression was used.
example:


$scope.items=[1,2,3,4,5,6]

<div ng-repeat="item in items">{{item}}</div>

or

<div ng-repeat="item in items track by $index">{{$index+1}}:{{item}}</div>
 here index start with the 0

In the angular 2 or more

<ul>
       <li> *ngFor="#item of items">{{item}}</li>
</ul>



Post a Comment

Previous Post Next Post