gpt4 book ai didi

javascript - 如何正确循环/ng-repeat 嵌套对象/数组?

转载 作者:行者123 更新时间:2023-11-29 19:04:32 25 4
gpt4 key购买 nike

我正在使用 angulars ng-repeat,我查找或尝试的任何东西都不起作用,我不明白为什么。我正在“循环”通过 companies.users 并希望显示所有名字。任何帮助都是极好的!非常感谢!!

<div ng-app="app" ng-controller="appCtrl">
<div ng-repeat="user in companies.users">
<p>{{user.firstName}}</p>
<div>

app.controller('appCtrl', function($scope){
$scope.companies = [{
name: 'The Best Company Denim',
users: [{
firstName: 'Alex',
lastName: 'D',
number: 1234
}, {
firstName: 'Sarah',
lastName: 't',
number: 14
}, {
firstName: 'J',
lastName: 'd',
number: 07
}]
}, {
name: 'The Best Company Elegant',
users: [{
firstName: 'Alx',
lastName: 'B',
number: 1234
}, {
firstName: 'Seth',
lastName: 'w',
number: 12
}, {
firstName: 'J.S',
lastName: 'B',
number: 7
}]
}, {
name: 'The Best Company by Julia',
users: [{
firstName: 'Aleddddx',
lastName: 'l',
number: 1234
}, {
firstName: 'Maggy',
lastName: 'n',
number: 1
}, {
firstName: 'Ja',
lastName: 'Key',
number: 123
}]
}]
});

最佳答案

您可以使用嵌套的 ng-repeat 来实现这一点。

<div ng-repeat="company in companies">
<div ng-repeat="user in company.users">
<p>{{user.firstName}}</p>
</div>
<div>

引用下面的例子:

angular.module("app", [])
.controller("myCtrl", function($scope) {
$scope.companies = [{
name: 'The Best Company Denim',
users: [{
firstName: 'Alex',
lastName: 'D',
number: 1234
}, {
firstName: 'Sarah',
lastName: 't',
number: 14
}, {
firstName: 'J',
lastName: 'd',
number: 07
}]
}, {
name: 'The Best Company Elegant',
users: [{
firstName: 'Alx',
lastName: 'B',
number: 1234
}, {
firstName: 'Seth',
lastName: 'w',
number: 12
}, {
firstName: 'J.S',
lastName: 'B',
number: 7
}]
}, {
name: 'The Best Company by Julia',
users: [{
firstName: 'Aleddddx',
lastName: 'l',
number: 1234
}, {
firstName: 'Maggy',
lastName: 'n',
number: 1
}, {
firstName: 'Ja',
lastName: 'Key',
number: 123
}]
}];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="app" ng-controller="myCtrl">
<div ng-repeat="company in companies">
<div ng-repeat="user in company.users">
<p>{{user.firstName}}</p>
</div>
</div>
</div>

关于javascript - 如何正确循环/ng-repeat 嵌套对象/数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43990923/

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