Search code examples
angularjsangular-ng-if

angularjs - ng-if with array key and result from function


I'm trying to use an ng-if within a repeater. Here's what I'm trying to do:

<li ng-repeat="timers in current.Timer | groupBy: 'day' | toArray:true | orderBy: '$key'" class="details-single-day" ng-click="editView('/edit')" ng-if="timers.$key==isToday()">
   <span class="details-day-name">Day: {{timers.$key}}</span>
   <bars timer="timers" day="timers.$key" title="Week"></bars>
</li>

isToday() function returns today as an int:

$scope.isToday = () ->
  return new Date().getDay()

Basically, I want to display my values if $key (from 0 to 6) is equal to isToday() result.

Without the ng-if, i can see all the details for everyday, including {{timer.day}} as int.

But with the ng-if, nothing at all is displayed.

Any help will be really appreciated. Thanks


Solution

  • I finally managed to solve this, and it was really simple.

    Instead of doing:

    <li ng-repeat="timers in current.Timer | groupBy: 'day' | toArray:true | orderBy: '$key'" class="details-single-day" ng-click="editView('/edit')" ng-if="timers.$key==isToday()">
       <span class="details-day-name">Day: {{timers.$key}}</span>
       <bars timer="timers" day="timers.$key" title="Week"></bars>
    </li>
    

    using a function (isToday), I did this instead:

    <li ng-repeat="timers in current.Timer | groupBy: 'day' | toArray:true | orderBy: '$key'" class="details-single-day" ng-click="editView('/edit')" ng-if="timers.$key == today">
       <span class="details-day-name">Day: {{timers.$key}}</span>
       <bars timer="timers" day="timers.$key" title="Week"></bars>
    </li>
    

    where today is not a function anymore:

    $scope.today = new Date().getDay()
    

    and now it works perfectly. Thanks to everyone for your help