ng-repeat上的Angular 4数据绑定 [英] Angular 4 Data Binding over ng-repeat
问题描述
我最近从Angular 1切换到了Angular 4,现在看来很多事情对我来说都是新的.其中之一似乎与数据绑定有关.在旧版本中,我将在JS控制器中将一个数组声明为$ scope.arrname,并且可以使用ng-repeat在HTML视图中对其进行导航.
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?
示例:在一个组件中,我声明了一个测试数组testarr:any [] = [1,2,3];
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?
推荐答案
您应使用 ngFor
而不是 ng-repeat
You should use ngFor
instead of ng-repeat
<ol>
<li *ngFor="let item of testarr">{{item}}ITEM Found!</li>
</ol>
这篇关于ng-repeat上的Angular 4数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!