简体   繁体   English

AngularJS - 通过数组的对象重复Ng

[英]AngularJS — Ng-repeat through objects of an array

I just started playing around with Angular JS a few days ago. 几天前我刚开始玩Angular JS。 I have this array called posts that has objects in it, which I want to be displayed and categorized by the year in which they have been posted. 我有一个名为posts数组,其中包含对象,我希望它们按照发布年份进行显示和分类。

This is the posts array: 这是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."
    }
];

As you can see, the posts array contains 3 objects, which represent posts. 如您所见, posts数组包含3个对象,代表帖子。 Each "post" has a title, date, and some content. 每个“帖子”都有标题,日期和一些内容。 I want to order them like so: 我想这样订购它们:

在此输入图像描述

This is what I currently have: 这就是我目前拥有的:

在此输入图像描述

I was able to access the year, but I can't get into the child objects. 我能够访问这一年,但我无法进入子对象。

This is what you see in the console when you do console.log($scope.postsByYear); 这是你在console.log($scope.postsByYear);中在控制台中看到的内容console.log($scope.postsByYear); : 在此输入图像描述

I wrote a script that puts the posts in the arrays based on the year in which they have been recorded. 我写了一个脚本,根据记录的年份将帖子放入数组中。 It just takes apart the post's date property, which has a date format of mm/dd/yy . 它只是拆分帖子的日期属性,日期格式为mm/dd/yy I don't know how to ng-repeat those objects within the posts array. 我不知道如何在posts数组中ng-repeat这些对象。

This is what I have: 这就是我所拥有的:

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

I have absolutely no idea why this code isn't working. 我完全不知道为什么这段代码不起作用。 It gets the date fine, but it cannot access the properties of the post objects. 它获取日期正常,但它无法访问post对象的属性。 It make sense, right? 这有道理,对吗? Post is a placeholder for one of the post objects, and postsByYear.posts should select the title in all the objects... Post是其中一个post对象的占位符, postsByYear.posts应该在所有对象中选择标题...

Here is the jsFiddle link to the code. 是代码的jsFiddle链接。 Your assistance is greatly appreciated. 非常感谢您的帮助。

Just a simple mistake. 只是一个简单的错误。 In your inner repeat, you're looping over the wrong object. 在你的内部重复中,你正在循环错误的对象。 Should be 应该

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM