简体   繁体   中英

How to change class depending on filter result in AngularJS?

I cannot work out how to change the style of a class depending on the status/result of the filter. My code:

    <div data-ng-if="search.length >= 3" >
      <div data-ng-class="{list:true}" style="margin-top: 30px;">
       <a class="item item_recipe" data-ng-repeat="item in items | nonBlankFilter:search | filter:search" data-ng-click="$emit('search.click',item.PK);">
        <img class="thumbnail" src="images/thumbnails/{{item.THUMB}}">
        <div class="title">{{item.TITLE}}</div>
    </a>
</div>

What is happening now, is the style "list" is still there which has a background and thus the background is still visible, even when there is no results in the filter.

I hope I have explained myself well.

You can assign your filter results to a intermediary variable and than use that variable to conditionally apply your class:

<div ng-if="search.length >= 3">
  <div ng-class="{list: filteredItems.length}" style="margin-top: 30px;">
    <a 
      class="item item_recipe" 
      ng-repeat="item in filteredItems = (items | nonBlankFilter:search | filter:search)" 
      ng-click="$emit('search.click',item.PK);"
    >
      <img class="thumbnail" src="images/thumbnails/{{item.THUMB}}">
      <div class="title">{{item.TITLE}}</div>
    </a>
  </div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM