gpt4 book ai didi

javascript - Angular ng-repeat 不迭代 json

转载 作者:行者123 更新时间:2023-11-28 14:56:39 27 4
gpt4 key购买 nike

我正在开发一个应用程序,其中一部分应该列出来自 javascript 对象的人员姓名。然而,它似乎并没有像我期望的那样迭代我的 json,而是返回整个事物,就好像它是一个对象一样。为什么要这样做?

坏掉的Plunker https://plnkr.co/edit/u9DHsTlaO0H2G2jqnzLa

索引.html

<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
<script src="app.js"></script>
</head>
<body>
<people-list></people-list>
</body>
</html>

app.js

var app = angular.module("plunker", []);
app.directive("peopleList", function() {
return {
restrict: "E",
templateUrl: "people-list.html",
controller: function() {
this.people = peopleObject;
},
controllerAs: "people"
};
});

var peopleObject = [
{ firstname: 'John',
middlename: 'K',
lastname: 'Doe',
sex: "M"
},
{
firstname: 'Jane',
middlename: 'R',
lastname: 'Doe',
sex: "F"
},
{
firstname: 'Jacob',
middlename: 'Harold',
lastname: 'Smith',
sex: "M"
}
];

people-list.html

<ul class="list-group menuIcons">
<li ng-click="tab.setTab(1);" class="list-group-item" ng-repeat="person in people">
{{person.firstname}}
</li>
</ul>

最佳答案

尝试对此进行更改。您在指令中定义 controllerAs 那么您应该在模板中使用它

<ul class="list-group menuIcons">
<li ng-click="tab.setTab(1);" class="list-group-item" ng-repeat="person in people.people">
{{person.firstname}}
</li>
</ul>

关于javascript - Angular ng-repeat 不迭代 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42619157/

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