好吧,我正在使用 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
集合绑定(bind)到您的 View ,并使用 ngClass
指令和 isNew
属性,例如
关于javascript - 在 Javascript AngularJS 中从旧帖子中确定新帖子,过滤与否,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26008363/