2014-09-24 46 views
0

好的,我使用WordPress和JSON API使WordPress成爲我的應用程序的後端,現在我正在處理從發佈日期和添加類,如果帖子是新的或不是,我不知道這是一個過濾器的工作,或只是擴大對象添加新的屬性,基本上遍歷帖子數組並添加屬性isNew確定在Javascript AngularJS的舊帖子的新帖,是否過濾

不能儘管我的邏輯正確。

比方說,我得到的職位從Post服務,像這樣

$scope.posts = []; 
Post.getPosts().then(function(posts){ 
    $scope.posts = posts; 
}); 

現在我做到這一點之前:$ scope.posts =的帖子,我想角延長每個崗位的性質,加入isNew

var today = new Date(); 
var newMark = new Date(); 
newMark.setDate(newMark.getDate() - 5); 

Post.getPosts(5).then(function (re) { 
    angular.forEach(re.posts, function(post, key){ 
    var postDate = new Date(post.date); 
    console.log(today - postDate < newMark); // lol, I don't have quite a logic here. 
    // console.log(postDate - today < today - newMark); 
    }); 
}); 

哈哈,今天好像不能想。

+0

你想要什麼邏輯這裏的console.log(今天 - 踵<紐馬克); – 2014-09-24 04:15:40

回答

3

你想設置該屬性爲true,其發佈日期是紐馬克後,所有的職位,不是嗎?

$scope.posts=[]; 
var newMark=new Date(Date.now() - 5 * 24 * 3600 *1000); 

Post.getPosts(5) 
.then(function (re) { 
    // add a property to the model to tell whether is new 
    $scope.posts= re.posts.map(function(post){ 
    post.isNew=new Date(post.date) >= newMark; 
    return post; 
    }); 
}); 

現在你可以在posts集合捆綁到您的視圖,並使用ngClass指令與isNew屬性例如

1

包括moment.js

Post.getPosts(5).then(function (re) { 
    var newMark = moment().subtract(5, 'days'); 
    for (var i in re.posts) { 
    re.posts[i].isNew = moment(post.date) > newMark ? true : false; 
    } 
}); 
+0

不錯的moment.js,但我現在不會使用它。 – 2014-09-24 04:35:56