使用 angular,如何在 ng-show 中调用自定义过滤器的结果?
With angular, how do i call the result of a custom filter in a ng-show?
我有一本包含一些数据的字典。
我正在使用自定义过滤器过滤这些数据,该过滤器查看当前日期并将其与日程安排的日期进行比较。如果当前日期在日程安排的日期之间,它会显示日程安排。
我的问题是,如果过滤后的 ng-repeat 的结果为 0,我想显示文本 "is now"。但我有一个错误,因为过滤器在 ng-repeat 中有效,但在其他地方无效。我可以做什么来根据过滤器的结果显示文本?任何帮助或设备都可能很棒。谢谢!
var events = [
{
id: 0,
location: {
location_lat: '45.769727',
location_long: '4.8314972',
},
schedules: [
{
id: 1,
start: '2015-05-28T14:00:00Z',
end: '2015-05-28T19:00:00Z',
},
{
id: 2,
start: '2015-05-30T19:00:00Z',
end: '2015-05-30T20:00:00Z',
},
{
id: 3,
start: '2015-05-30T20:00:00Z',
end: '2015-05-30T21:00:00Z',
}
]
}]
.filter('isNow', function (){
return function (schedules) {
var result = [];
angular.forEach(schedules, function (schedule, key) {
var currentDate = new Date();
var scheduleStart = new Date(schedules[key].start);
var scheduleEnd = new Date(schedules[key].end);
if ( (currentDate >= scheduleStart) && (currentDate <= scheduleEnd) ) {
result.push(schedule);
}
}, result);
return result;
}
})
<ion-item class="" ng-repeat="location in locations" type="item-text-wrap">
<div>
<p>{{location.location.location_distance}}</p>
</div>
<div class="now">
<p ng-show="([schedules] | isNow).length" > 0"Is now</p>
<div class="" ng-repeat="schedule in location.schedules | isNow">
<p>{{schedule.start | date:'H:mm'}}</p>
</div>
</div>
<div class="next" >
<p>À venir</p>
<div class="" ng-repeat="schedule in location.schedules">
<p>{{schedule.start | date:'H:mm'}}</p>
<p>{{schedule.genre}}</p>
</div>
</div>
</ion-item>
您可以将 $filter 注入您的控制器,创建一个函数并将您的 ng-show 绑定到该函数:
How to use a filter in a controller?
$scope.checkIsNow = function(item){
$filter('isNow')(item);
return true|false;
};
ng-show="checkIsNow(schedules)"
我有一本包含一些数据的字典。
我正在使用自定义过滤器过滤这些数据,该过滤器查看当前日期并将其与日程安排的日期进行比较。如果当前日期在日程安排的日期之间,它会显示日程安排。
我的问题是,如果过滤后的 ng-repeat 的结果为 0,我想显示文本 "is now"。但我有一个错误,因为过滤器在 ng-repeat 中有效,但在其他地方无效。我可以做什么来根据过滤器的结果显示文本?任何帮助或设备都可能很棒。谢谢!
var events = [
{
id: 0,
location: {
location_lat: '45.769727',
location_long: '4.8314972',
},
schedules: [
{
id: 1,
start: '2015-05-28T14:00:00Z',
end: '2015-05-28T19:00:00Z',
},
{
id: 2,
start: '2015-05-30T19:00:00Z',
end: '2015-05-30T20:00:00Z',
},
{
id: 3,
start: '2015-05-30T20:00:00Z',
end: '2015-05-30T21:00:00Z',
}
]
}]
.filter('isNow', function (){
return function (schedules) {
var result = [];
angular.forEach(schedules, function (schedule, key) {
var currentDate = new Date();
var scheduleStart = new Date(schedules[key].start);
var scheduleEnd = new Date(schedules[key].end);
if ( (currentDate >= scheduleStart) && (currentDate <= scheduleEnd) ) {
result.push(schedule);
}
}, result);
return result;
}
})
<ion-item class="" ng-repeat="location in locations" type="item-text-wrap">
<div>
<p>{{location.location.location_distance}}</p>
</div>
<div class="now">
<p ng-show="([schedules] | isNow).length" > 0"Is now</p>
<div class="" ng-repeat="schedule in location.schedules | isNow">
<p>{{schedule.start | date:'H:mm'}}</p>
</div>
</div>
<div class="next" >
<p>À venir</p>
<div class="" ng-repeat="schedule in location.schedules">
<p>{{schedule.start | date:'H:mm'}}</p>
<p>{{schedule.genre}}</p>
</div>
</div>
</ion-item>
您可以将 $filter 注入您的控制器,创建一个函数并将您的 ng-show 绑定到该函数:
How to use a filter in a controller?
$scope.checkIsNow = function(item){
$filter('isNow')(item);
return true|false;
};
ng-show="checkIsNow(schedules)"