0

I have a dictionary with some datas.

I'm filtering these datas with a custom filter which looks the current date and compares it with the date of the schedule. If the current date is between the dates of the schedule, it displays the schedules.

My problem is I want to display a text "is now" if the result of the filtered ng-repeat is 0. But I have an error because, the filter works in the ng-repeat but not elsewhere. What can I do to display the text depending on the result of the filter? Any help or device could be wonderful. Thanks !

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>
Okyne
  • 95
  • 9

1 Answers1

1

You would inject $filter into your controller, make a function and bind your ng-show to that:

How to use a filter in a controller?

$scope.checkIsNow = function(item){
    $filter('isNow')(item);
    return true|false;
};

ng-show="checkIsNow(schedules)"
Community
  • 1
  • 1
Kevin F
  • 2,836
  • 2
  • 16
  • 21