How to change class depending on filter result in AngularJS?

和自甴很熟 提交于 2019-12-02 16:13:31

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/20687586/how-to-change-class-depending-on-filter-result-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!