在AngularJS中,如何在过滤器过滤器函数中使用参数?

时间:2017-07-14 14:17:53

标签: angularjs function ionic-framework filter parameters

<div ng-repeat="city in cities">
  <div>city: {{city}}</div>

  <div ng-repeat="contact in contacts | filter:selectItems(city)">
    contact: {{contact.name}}
  </div>

  <hr>
</div>


angular.module("myApp", [])
  .controller("MyCtrl", function ($scope) {
    $scope.selectItems = function (item, city) {
      return item.city === city;
    };
  });

它不起作用。因为过滤器过滤器功能中有一个参数 city 。但在这种情况下,在过滤器过滤器函数中使用参数的正确语法是什么?或者过滤器过滤器功能中是否允许参数?

1 个答案:

答案 0 :(得分:0)

尝试使用自定义过滤器

yourmodule.filter('selectItems', function () {
  return function (items, city) {
    //your logic
  };
});


<div ng-repeat="contact in contacts | selectItems:city">
    contact: {{contact.name}}
  </div>

其他一些具有相同情景的参考资料 elfutils