Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 Data Binding over ng-repeat

Tags:

angular

I've recently switched to Angular 4 from Angular 1 and lots of things seem to be new to me now. One of them seems to relate to data-binding. In the old version, I would declare an array as $scope.arrname in the JS controller and I could navigate over it in the HTML view using ng-repeat.

Now, when I am trying to achieve the same outcome, it only works partially. What am I doing wrong?

Example: In a component, I declared a test array testarr : any[] = [1,2,3];

{{testarr}}
   > Prints 1,2,3 on the scrreen 


<ol>
  <li ng-repeat="item in testarr">{{item}}ITEM Found!</li>
</ol>


>only iterates 1 time (ignoring the 2,3) in the array.

Why does my code not iterate over the array as it was the case previously ? What am I missing here?

like image 220
afabijan Avatar asked Jun 02 '17 02:06

afabijan


People also ask

What can I use instead of NG-repeat?

The *ngFor directive in Angular is similar to the ng-repeat directive in AngularJS. It repeats the associated DOM element for each item in the specified collection.

Does ng-repeat create a new scope?

Each iteration of ng-repeat creates a new child scope, and that new child scope always gets a new property.

What is the use of NG-repeat in angular?

AngularJS ng-repeat Directive The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.


1 Answers

You should use ngFor instead of ng-repeat

<ol>
  <li *ngFor="let item of testarr">{{item}}ITEM Found!</li>
</ol>
like image 170
Sajeetharan Avatar answered Oct 02 '22 06:10

Sajeetharan