使用 ng-repeat 在 html 中打印数组

Printing out array in the html with ng-repeat

我的控制器中有一组双打(它们最终变成双打),我正尝试使用 ng-repeat 在我的网页上打印,但我不确定如何打印。该数组只是":

vm.calorieArray = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

其中 vm 是我的控制器的别名。

在我的 html 中,我有一个看起来像这样的 div 试图打印出来:

<div ng-repeat="value in vm.calorieArray">
    {{value}}<br />
</div>

但页面上没有显示任何内容。有人看到问题了吗?

这里是一个 fiddle,你的例子有效:http://jsfiddle.net/JKBbV/743/

控制器很简单:

myApp.controller("myCtrl", function($scope) {
    $scope.values = [0,0,0,0,0.1];
});

我的猜测是您的应用没有正确初始化,或者您没有正确地将数组绑定到 $scope

问题可能是因为 ng-repeat 中不允许重复值。参见 this plunker for an example of the issue. (Open up your dev console to see the JS errors) The description of the error can be found here

简而言之,您应该使用 'track by $index',如下所示:

<div ng-repeat="value in calorieArray track by $index">
    {{value}}<br />
</div>