AngularJS ng重复过滤器,对象属性上的函数
我正在寻找一种方法来检查每部电影是否有被选中的类别。Movies是一个包含对象的数组,这些对象具有一些属性,如下面的代码所示。“类别”属性是电影所在的类别数组。现在有一个变量selectedCategories,当前选定的类别存储在其中 我不想使用自定义过滤器,因为我认为这是可能的,我只是不能完全得到它。在javascript函数中也不能有太多的更改 如果hasSelectedCategory的返回值为true,则必须执行该块,如果返回值为false,则必须执行该块 提前谢谢AngularJS ng重复过滤器,对象属性上的函数,angularjs,angularjs-ng-repeat,angular-filters,Angularjs,Angularjs Ng Repeat,Angular Filters,我正在寻找一种方法来检查每部电影是否有被选中的类别。Movies是一个包含对象的数组,这些对象具有一些属性,如下面的代码所示。“类别”属性是电影所在的类别数组。现在有一个变量selectedCategories,当前选定的类别存储在其中 我不想使用自定义过滤器,因为我认为这是可能的,我只是不能完全得到它。在javascript函数中也不能有太多的更改 如果hasSelectedCategory的返回值为true,则必须执行该块,如果返回值为false,则必须执行该块 提前谢谢 //in the
//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>
//在javascript文件中
scope.hasSelectedCategory=功能(类别){
var hasCategory=false;
if(categories.indexOf(scope.selectedCategory)!=-1){
hasCategory=true;
}
返回类别;
};
//在html文件中
{{movie.title}
{{movie.description}}
您必须像这样使用过滤器:
ng-repeat="movie in movies | filter:hasSelectedCategory"
$scope.hasSelectedCategory = function(movie) {
var hasCategory = false;
angular.forEach($scope.selectedCategories, function(selectedCategory) {
if (!hasCategory && movie.categories.indexOf(selectedCategory) !== -1) {
hasCategory = true;
}
});
return hasCategory;
};
hasSelectedCategory
功能将为movies
列表中的每个movie
调用。要按所选类别进行筛选,可以使用如下函数:
ng-repeat="movie in movies | filter:hasSelectedCategory"
$scope.hasSelectedCategory = function(movie) {
var hasCategory = false;
angular.forEach($scope.selectedCategories, function(selectedCategory) {
if (!hasCategory && movie.categories.indexOf(selectedCategory) !== -1) {
hasCategory = true;
}
});
return hasCategory;
};