如何过滤angularjs中的值?

时间:2017-04-07 08:51:42

标签: html angularjs meanjs

大家好我怎样才能过滤angularjs中的值?

  • 我创建了plunker供参考: - My Plunker

  • 我想在ng-repeat user

  • 中过滤categories question list page

用户数据: -

"user": {
"_id": "58072aba0f82a61823c434df",
"displayName": "Table 1",
"dob": "2016-12-22T18:30:00.000Z",
"location": "chennai",
"religion": "hindu",
"roles": [
"admin"
],
"profileImageURL": "./modules/users/client/img/profile/uploads/ac4fbab396c2f725ed5211524f171136"
},
  • categories值在数组中,我只需要在ng-repeat列表页面中过滤categories值....

  • 例如: - 如果user categories values "categories": [ "Religion & Culture", "Social Psychology" ],,那么这两个值只应在类别列表中过滤...

我的Html: -

    <div ng-repeat="question in questions | filter:user.categories ">
    <small>
                    <span >{{$index + 1}}.</span>
                      <span data-ng-bind="question.category"></span>
                  </small>

  </div>
  • 我在ng-repeat中使用了过滤器,如: - | filter:user.categories

  • 我想在重复列表中过滤用户类别...

  • 请检查并更新plunker以了解确切的解决方案......

2 个答案:

答案 0 :(得分:1)

您需要创建自定义过滤器,如:

.filter('filterCategory', [
 function() {
  return function(questions) {
   var filteredCategories = [];
   if (questions && questions.length) {
    angular.forEach(questions, function(question) {
      console.log(question)
      if(question.user && question.user.categories && question.user.categories.length){
        angular.forEach(question.user.categories, function(category){
          if(filteredCategories.indexOf(category)<0){
            filteredCategories.push(category);
          }
        })
      }
    })
  }
   return filteredCategories;
 }
}

])

然后在ng-repeat中你需要写:

<div ng-repeat="category in questions| filterCategory ">

答案 1 :(得分:0)

而不是filter: user.categories尝试filter: question.user.categories