好的,我正在使用WordPress和JSON API将WordPress用作应用程序的后端,现在我正在处理从帖子获取日期并添加类(无论帖子是否是新的),我不知道这是否是一项工作过滤器或只是扩展对象以添加new属性,基本上遍历帖子数组并添加isNew
属性
虽然我的逻辑不对。
可以这样说,我从Post
服务获取帖子
$scope.posts = [];
Post.getPosts().then(function(posts){
$scope.posts = posts;
});
现在在执行此操作之前:$ scope.posts = posts,我希望Angular扩展每个帖子的属性,并添加
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);
});
});
大声笑,今天似乎无法思考。
最佳答案
您想要将所有发布日期在newMark之后的帖子的属性设置为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
指令,例如关于javascript - 从Javascript AngularJS中的旧帖子确定新帖子,是否过滤,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26008363/