如何在空字符串值上过滤ng-repeat列表?

时间:2017-07-20 12:42:28

标签: javascript angularjs angularjs-ng-repeat angular-filters

如何过滤ng-repeat以显示某个列字段为空字符串的所有项目?当我尝试这个时,似乎总是给出完整列表。我只想看到身份1的人。

Fiddlejs example

控制器:

public class GenericArrayTest<TKey extends Comparable,TValue>
                                   implements Iterable<Map.Entry<TKey,TValue>> {
  private TKey[] _arrayCOll;

  public void test(){
    _arrayCOll = new TKey[10];//error "Cannot create a generic array of TKey"
  }

  @Override
  public Iterator<Entry<TKey, TValue>> iterator() {
        // TODO Auto-generated method stub
        return null;
  }

}

查看:

var people = [{
                name: '',
                age: 32,
                id: 1
            }, {
                name: 'Jonny',
                age: 34,
                id: 2
            }, {
                name: 'Blake',
                age: 28,
                id: 3
            }, {
                name: 'David',
                age: 35,
                id: 4
            }];

 $scope.filteredPeople = $filter('filter')(people, {
     name: ''
 });

 $scope.people = people.slice(0);

3 个答案:

答案 0 :(得分:2)

您可以在'filter'中使用Angular的'ng-repeat'

// In template:
<li ng-repeat="p in filteredPeople | filter : filterPeople">
     <h4>{{p.name}} ({{p.age}}) id: {{p.id}}</h4>
</li>

// In controller:
$scope.filterPeople = function(item) {
    return !item.name;
};

答案 1 :(得分:1)

列出名称为

的用户
<li ng-repeat="p in filteredPeople" ng-if="p.name !== ''">
     <h4>{{p.name}} ({{p.age}}) id: {{p.id}}</h4>
</li>

仅列出没有名称的用户:

<li ng-repeat="p in filteredPeople" ng-if="p.name === ''">
     <h4>{{p.name}} ({{p.age}}) id: {{p.id}}</h4>
</li>

答案 2 :(得分:0)

只需传递第三个参数即可过滤说true,这将执行严格检查

$scope.filteredPeople = $filter('filter')(people, {
    name: ''
}, true);

http://localhost/WebUI/Scripts/uicontrols/basic/fastsearch.js