Angular 4 Data Binding over ng-repeat

2020-04-02 08:38发布

问题:

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?

回答1:

You should use ngFor instead of ng-repeat

<ol>
  <li *ngFor="let item of testarr">{{item}}ITEM Found!</li>
</ol>


标签: angular