Search code examples
angularjsangularjs-ng-repeatangular-ng-ifmd-chip

Unable to use ng-if with ng-repeat $index


I'm trying to use ng-if to display a button based on filters being present on screen. I'm using md-chips to show filters in categories.

HTML:

<div ng-repeat="filter in sc.filters track by $index">
  <md-chips ng-model="filter.value" ng-if="sc.isArray(filter.value)" md-on-remove="sc.filter()"></md-chips>
</div>
<button ng-if="$first" ng-click="sc.newSearch()">clear filters</button>

I track the index and try to add the button if first item was present. It doesn't show the button at all. Any help appreciated.


Solution

  • $first should be used within an ng-repeat statement. In this case you don't want to use $first though.

    If you want to add the button if the first item is present, why not just do something like this:

    <button ng-if="sc.filters.length" ng-click="sc.newSearch()">clear filters</button>