gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:34:23 24 4
gpt4 key购买 nike

过去两天我尝试解决了一个小问题。

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

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

有人可以帮我吗?

索引.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']}
];

});

非常感谢

最佳答案

你必须使用 track by 表达式来告诉 Angular 元素是不同的。尝试

<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>

关于javascript - angularjs,具有相同值的嵌套数组中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26572447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com