gpt4 book ai didi

javascript - AngularJS — Ng 重复数组对象

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

我几天前才开始使用 Angular JS。我有一个名为 posts 的数组,其中包含对象,我希望按发布年份显示和分类这些对象。

这是 posts 数组:

var posts = [
{
title: "First Blog Post of 2015",
date: "06/24/15",
content: "This is the first post of the year 2015."
}, {
title: "Second Blog Post of 2015",
date: "07/01/15",
content: "This is the second post of the year 2015."
}, {
title: "First Blog Post of 2014",
date: "07/11/14",
content: "This is the first post of the year 2014."
}
];

如您所见,posts 数组包含 3 个对象,代表帖子。每个“帖子”都有一个标题、日期和一些内容。我想这样订购它们:

enter image description here

这是我目前拥有的:

enter image description here

我能够访问年份,但无法访问子对象。

这是您执行 console.log($scope.postsByYear); 时在控制台中看到的内容: enter image description here

我编写了一个脚本,根据记录的年份将帖子放入数组中。它只是拆开帖子的日期属性,日期格式为 mm/dd/yy。我不知道如何ng-repeat posts 数组中的那些对象。

这是我的:

<div>
<h1>Blog</h1>
<ul ng-repeat="year in postsByYear">
<li>{{year.year}}</li>
<li ng-repeat="post in postsByYear.posts">{{post.title}}</li>
</ul>
</div>

我完全不知道为什么这段代码不起作用。它可以很好地获取日期,但无法访问帖子对象的属性。这是有道理的,对吧? Post 是其中一个 post 对象的占位符,postsByYear.posts 应该选择所有对象中的标题...

Here是代码的 jsFiddle 链接。非常感谢您的协助。

最佳答案

只是一个简单的错误。在你内心的重复中,你在错误的对象上循环。应该是

<li ng-repeat="post in year.posts">{{post.title}}</li>

关于javascript - AngularJS — Ng 重复数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31373858/

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