我正在尝试使用时间戳使用angularjs来命令一个json数组,但它不能用于somereson
<div ng-repeat="post in posts track by post.id|orderBy:'post_date':reverse" class="list-group-item apost">
<h4 class="timeline-title">{{post.title}}</h4>
</div>
JS
$scope.Posts = [
{"id":1,"user_id":1,"title":"Welcome to MRI","body":"\u0641\u064a \u062d\u0627\u0644\u0647 \u0627\u064a \u0627\u0633\u062a\u0641\u0627","post_date":1422929860,"fullname":"MZ"},
{"id":3,"user_id":1,"title":"\u0641\u064a \u062d\u0627\u0644\u0647 \u0636\u0627,","post_date":1422933651,"fullname":"MZ"},
{"id":24,"user_id":1,"title":"\u0641\u064a \u062d\u0627\u0644\u0647 \u0627\u064a \u0627\u0633\u062a\u0641\u0633\u0627\u0631","body":"\u0628\u0631\u0631","post_date":1425404937,"fullname":"Mz"},
{"id":29,"user_id":1,"title":"new post","body":"fk shit mf","post_date":1425405333,"fullname":"MZ"}
]
我想先显示最新的帖子。
是否可以检查post_date今天是否在角度模板中应用特殊类?
答案 0 :(得分:1)
根据this answer,您必须在track by
声明后使用orderBy
。
从ngRepeat
文档(参见'参数'表):
例如:
item in items | filter:searchText track by item.id
是一个 可能用于将过滤器应用于项目的模式 跟踪表达。
根据这些信息,我想你想要使用类似的东西:
post in posts | orderBy:'post_date':true track by post.id
也可以检查post_date今天是否有角度 模板应用特殊类?
为此,您可以定义一个函数来检查您的值是否在今天$scope
。
Angular JS:
.controller("PostCtrl", function($scope) {
$scope.isToday = function(date) {
/**
* Logic to determine if date is today or not goes here.
* Will either return true or false.
*/
};
})
然后在您的HTML模板中,您可以将每个帖子的日期传递到isToday
。如果它返回true,那么你的specialClass将被应用。
HTML:
<div ng-repeat="post in posts | orderBy:'post_date':true track by post.id" class="list-group-item apost">
<h4 class="timeline-title" ng-class="{specialClass: isToday(post.post_date)}">{{post.title}}</h4>
</div>