Search code examples
javascriptangularjsangularjs-ng-repeatangular-filters

how to ng-repeat values for result from filter


What is the best implementation for ng-repeat some items from array with depending for property (like: hidden: true).

var items = [
    {
       label: 'Fist item',
    },
    {
       label: 'Second item',
       hidden: true
    },
    {
       label: 'Third item',
    },
]

For result i want to see Fist & Third items that don't have hidden property.


Solution

  • You can use the filter filter, which, as it says, will filter your array according to conditions you give it.

    <div ng-repeat="item in items | filter:{hidden:false}">
      ...
    </div>
    

    Read the documentation for more available filter values.

    Another example would be to use a function in the scope, if you need better manipulation:

    $scope.shouldFilter = function(item) {
      return item.hidden === false || !('hidden' in item);
    }
    

    <div ng-repeat="item in items | filter:shouldFilter">
      ...
    </div>