How to use ng-if with ng-repeat?

Chris提出了一个问题:How to use ng-if with ng-repeat?,或许与您遇到的问题类似。

回答者Cuong Vo给出了该问题的处理方式:

There's probably a better solution, but after reading the replies above, you can try making your own custom filter:

angular.module('yourModule').filter('filterNavItems', function() {
  return function(input) {
    var inputArray = [];

    for(var item in input) {
      inputArray.push(input[item]);
    }

    return inputArray.filter(function(v) { return v.nav; });
  };
});

Then to use it:

<section class="nav">
    <a  ng-repeat="(key, item) in routes | filterNavItems"
        ng-href="{{key}}">
            {{item.label}}
    </a>
</section>

Here's the Plunker: http://plnkr.co/edit/srMbxK?p=preview

希望本文对你有帮助,欢迎支持JavaScript中文网