在ng-repeat内过滤

时间:2015-04-25 07:16:56

标签: angularjs angularjs-ng-repeat

我有一个对象:

$scope.fields = {

    id:                     'uid',
    email:                  'mail',
    houseNumber:            'det.1',
    streetOne:              'det.2',
    streetTwo:              'det.3',
    county:                 'det.4',
    postcode:               'det.5',
    daytimeNumber:          'det.6',
    firstName:              'det.7',
    lastName:               'det.8',         
    fullName:               ['firstName', 'lastName'],
    address:                ['houseNumber', 'streetOne', 'streetTwo', 'county', 'postcode'],
    mobile:                 'det.9',
    subscribed:             'det.10',
    yearOfBirth:            'det.19',       
    model:                  'model',            
    serial:                 'serial',
    purchaseDate:           'purchase_date',
    registrationDate:       'registraition_date'

};

和一个数组:

$scope.chosenFields = ['id', 'email', 'fullName', 'postcode', 'subscribed', 'address'];

在我的HTML中,我想显示$ scope.fields中的键(例如model,serial,purchaseDate),这些键不在$ scope.chosenFields数组中。所以我这样做:

<ul>
    <li ng-repeat="(key, value) in fields | filter:{checkFields:true}">{{key}}</li>
</ul>

在我的checkFields过滤器中,我根本没有接到电话。这是否是一种正确的做法?

2 个答案:

答案 0 :(得分:0)

如果checkFields是过滤器,则调用它的方式是

... | checkFields:true

您当前的代码调用角度过滤器filter,并将其作为参数传递给以下对象

{
    checkFields: true
}

答案 1 :(得分:0)

我找到了一个基于此的解决方案: AngularJS - How to structure a custom filter with ng-repeat to return items conditionally

app.filter('checkFields', function(){
   return function(fields, globalFields){
       var filtered = [];
       angular.forEach(fields, function(element, index){
           if(globalFields.indexOf(index)<0){
              filtered.push(index);
           }
       });
      return filtered;
   }
});