AngularJS : Custom filters and ng-repeat

前端 未结 4 1973

I\'m an AngularJS newbie and I\'m building up a small proof-of-concept car hire listings app that pulls in some JSON and renders out various bits of that data via an ng-repe

4条回答
  •  广开言路
    2020-11-27 11:46

    If you still want a custom filter you can pass in the search model to the filter:

    Where definition for the cartypefilter can look like this:

    app.filter('cartypefilter', function() {
      return function(items, search) {
        if (!search) {
          return items;
        }
    
        var carType = search.carType;
        if (!carType || '' === carType) {
          return items;
        }
    
        return items.filter(function(element, index, array) {
          return element.carType.name === search.carType;
        });
    
      };
    });
    

    http://plnkr.co/edit/kBcUIayO8tQsTTjTA2vO?p=preview

提交回复
热议问题