我有一个类似的JSON数组:
{
"date": "01/01/2016",
"duration": "duration : 1h30",
"hour": "18:30",
"subject": "subject",
"price": "free",
"link": "link",
"link/_text": "something",
"description": "something",
"location": "somewhere"
},
{
"date": "02/01/2016",
"duration": "duration : 1h",
"hour": "11:00",
"subject": "subject",
"price": "free",
"link": "something",
"link/_text": "something",
"description": "something",
"location": "somewhere"
},
{
"date": ["26/01/2016","27/01/2016"],
"duration": "duration : 2 days",
"hour": "18:30",
"subject": "subject",
"price": "free",
"link": "link",
"link/_text": "something",
"description": "something",
"location": "somewhere"
}....
我想知道我有两天的活动。
使用angularjs& ionicframework我能够使用{{countDates(event.date)}}函数:
$scope.countDates = function(eventObj) {
return Object.keys(eventObj).length;
};
它通常返回唯一日期键的数量,所以当我获取10个项目时它返回10 ...但是在“to ... from”日期它返回2,所以我能够构建一个模板: / p>
<div collection-repeat="event in events | limitTo:rssnb" class="item item-avatar item-text-wrap" ng-click="browse(event.link)">
<div ng-if="countDates(event.date) == 2">
<b>from</b> {{event.date[0]}} <b>to</b> {{event.date[1]}} </br>
at {{event.hour}}<br />
</div>
<div ng-if="countDates(event.date) != 2">
on the {{event.date}} at {{event.hour}}<br />
</div>
</div>
我认为是:
您能否建议更好的方法来检测键值是唯一的还是双倍的?
非常感谢,
YTS
答案 0 :(得分:0)
我认为最好将JSON中的日期保存为类似于具有两个日期的数组:
{
"date" : [...],
...
}
您可能想要更改代码:
<div ng-repeat="event in events | limitTo:rssnb" class="item item-avatar item-text-wrap" ng-click="browse(event.link)">
<div ng-if="countDates(event.date) > 1">
<b>from</b> {{event.date[0]}} <b>to</b> {{event.date[1]}} </br>
at {{event.hour}}<br />
</div>
<div ng-if="countDates(event.date) == 1">
on the {{event.date[0]}} at {{event.hour}}<br />
</div>
但是如果你想要一个唯一的日期(如果那是你在问题中提到的那个),那么你可能想要使用angular.filter模块中的unique
过滤器,它将给出唯一的值....
答案 1 :(得分:0)
由于你使用角度,你可以使用它:
<div collection-repeat="event in events | limitTo:rssnb" class="item item-avatar item-text-wrap" ng-click="browse(event.link)">
<div ng-if="angular.isArray(event.date) && countDates(event.date) == 2">
<b>from</b> {{event.date[0]}} <b>to</b> {{event.date[1]}} </br>
at {{event.hour}}<br />
</div>
<div ng-if="!angular.isArray(event.date) || (angular.isArray(event.date) && countDates(event.date) != 2)">
on the {{event.date}} at {{event.hour}}<br />
</div>
</div>
但是你也应该将ng-if中的所有逻辑移到控制器中以保持视图清洁。