angularjs,具有相同值的嵌套数组中的错误

时间:2014-10-26 11:26:46

标签: javascript arrays angularjs

我在最后两天尝试了一个小问题。

我是AngularJS的新手,我不明白为什么这不起作用。

如果数组中的值相同,则嵌套的ng-repeat不起作用:(

有人可以帮助我吗?

index.html:

<!DOCTYPE html>
<html ng-app="testApp">
<head>
    <title></title>
</head>
<body ng-controller="testCtrl">
    <ul>
        <li ng-repeat="item in items">
            {{item.ip}}
            <ul>
                <li ng-repeat="s in item.status">{{s}}</li>
            </ul>
        </li>
    </ul>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
    <script src="app.js"></script>
</body>
</html>

app.js(这是有效的)

var app = angular.module('testApp', []);

app.controller('testCtrl', function ($scope) {

$scope.items = [
    {ip: '192.168.1.23', status: ['aaa','bbb','ccc','ddd','eee','fff']},
    {ip: '192.168.1.24', status: ['ggg','hhh','iii','jjj','kkk','lll']}
];

});

app.js(没有工作)

var app = angular.module('testApp', []);

app.controller('testCtrl', function ($scope) {

$scope.items = [
    {ip: '192.168.1.23', status: ['na','up','na','down','down','na']},
    {ip: '192.168.1.24', status: ['up','down','na','up','up','na']}
];

});

非常感谢

2 个答案:

答案 0 :(得分:2)

您必须使用track by表达式来表示元素不同的角度。尝试

<ul>
    <li ng-repeat="item in items">
        {{item.ip}}
        <ul>
            <li ng-repeat="s in item.status track by $index">{{s}}</li>
        </ul>
    </li>
</ul>

答案 1 :(得分:1)

其中有相同的值将“track by $ index”附加到ng-repeat,如

  ng-repeat="s in item.status track by $index"