javascript - 在 Javascript AngularJS 中从旧帖子中确定新帖子,过滤与否

标签 javascript php wordpress angularjs

好吧,我正在使用 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/

相关文章:

javascript - 使用 jQuery 和 AJAX 进行长轮询的正确方法是什么

javascript - 将 ng-bootstrap 添加到 Angular 应用程序对 HTML 控件没有影响

javascript - 如何从生成的 JSON 数组中提取 JSON 对象?

css - 主题移动填充问题

javascript - 如何修改wordpress插件变量值

javascript - 鼠标悬停时动画元素向下滑动

php - 根据变量创建多个div

javascript - 如何将多个复选框值从一个文件显示到另一个文件的文本框

php - 在 php 中为 $_POST 刷新一次

html - 缩小的 css 在 wordpress 中被 bootstrap cdn 覆盖