gpt4 book ai didi

javascript - ng-repeat 的问题

转载 作者:行者123 更新时间:2023-11-30 12:13:40 25 4
gpt4 key购买 nike

我正试图掌握 AngularJS 并让它工作以从 Controller 回显个别事物,但是当我尝试将循环与 ng-repeat 合并时,我得到的只是 {{tasks.title}} 在屏幕上闪烁一秒钟,然后是一个空白的空 div。我查过的任何例子似乎都有些不同,我已经尝试了几种方法,有人能看出我哪里出错了吗?

Controller :

app.controller('MainController', ['$scope', function($scope) {
$scope.tasklist = {
tasks: [{
title: 'Wash Dog'
}, {
title: 'Email Joe'
}]};
}
]);

HTML:

<section class="panel">
<div ng-controller="MainController">
<div class="thumbnail" ng-repeat="tasks in tasklist">
<p>{{ tasks.title }}</p>
</div>
</div>
</section>

最佳答案

您访问的是属性任务列表,而不是实际任务。应该是tasklist.tasks

<section class="panel">
<div ng-controller="MainController">
<div class="thumbnail" ng-repeat="tasks in tasklist.tasks">
<p>{{ tasks.title }}</p>
</div>
</div>
</section>

另一种方法是删除 tasks 属性:

app.controller('MainController', ['$scope', function($scope) {
$scope.tasklist = [
{
title: 'Wash Dog'
},
{
title: 'Email Joe'
}
];
}]);

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

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