Filtering on object map rather than array in AngularJS

前端 未结 6 2122
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 05:15

Given a controller with a $scope property that is an object with other properties rather than an array like below, how should I filter the ng-repeat set?

6条回答
  •  春和景丽
    2020-12-03 05:36

    I made an example on how to not change your object to an Array. I think this answers the question more correct.

    I had the same problem that i could not search in a Object.

    http://jsbin.com/acagag/223/edit

    angular.module('filters',['utils'])
      .filter('friendFilter', function(utils){
    
        return function(input, query){
          if(!query) return input;
          var result = {};
    
          angular.forEach(input, function(friendData, friend){
            if(utils.compareStr(friend, query) ||
               utils.compareStr(friendData.phone, query))
              result[friend] = friendData;          
          });
          return result;
        };
      });
    

    So just instead of returning a array you return a object.

    Hope this helps someone!

提交回复
热议问题