【发布时间】:2014-09-18 21:01:31
【问题描述】:
我正在寻找一种方法来检查每部电影是否具有选定的类别。 Movies 是一个包含对象的数组,这些对象具有一些属性,如下面的代码所示。类别属性是电影所在类别的数组。现在有一个变量 selectedCategories 存储当前选择的类别。
我不想使用自定义过滤器,因为我认为这有可能,我只是不太明白。在javascript函数中也不能改变太多。
如果hasSelectedCategory的返回为真,则必须执行该块,如果不为假。
提前致谢。
//in the javascript file
scope.hasSelectedCategory = function(categories){
var hasCategory = false;
if (categories.indexOf(scope.selectedCategory) !== -1){
hasCategory = true;
}
return hasCategory;
};
//in the html file
<div class="movieListItem {{listItemView}}" ng-repeat="movie in movies | filter:{hasSelectedCategory(categories): true}">
<h4>{{movie.title}}</h4>
<a href="http://www.youtube.com/embed/{{movie.youtubeId}}?rel=0&autoplay=1">
<img ng-src="{{findPosterSource(movie)}}" class="poster"> </a>
<p ng-hide="listItemView === 'grid'">
{{movie.description}}
</p>
<div class="categories" >
<span ng-repeat="category in movie.categories"> <a ng-href="#">{{category}}</a> </span>
</div>
</div>
【问题讨论】:
标签: angularjs angularjs-ng-repeat angular-filters