在AngularJS中,如何在过滤函数中使用参数? 城市:{{city} 联系人:{{contact.name} angular.module(“myApp”,[]) .controller(“MyCtrl”,函数($scope){ $scope.selectItems=功能(项目,城市){ 返回项。城市===城市; }; });

在AngularJS中,如何在过滤函数中使用参数? 城市:{{city} 联系人:{{contact.name} angular.module(“myApp”,[]) .controller(“MyCtrl”,函数($scope){ $scope.selectItems=功能(项目,城市){ 返回项。城市===城市; }; });,angularjs,function,ionic-framework,filter,parameters,Angularjs,Function,Ionic Framework,Filter,Parameters,它不起作用。因为过滤函数中有一个参数城市。但是在这种情况下,在filter函数中使用参数的正确语法是什么?或者过滤器功能中是否允许使用参数?尝试使用自定义过滤器 <div ng-repeat="city in cities"> <div>city: {{city}}</div> <div ng-repeat="contact in contacts | filter:selectItems(city)"> contact: {{c

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

尝试使用自定义过滤器

<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;
    };
  });
yourmodule.filter('selectItems',function(){
返回功能(项目、城市){
//你的逻辑
};
});
联系人:{{contact.name}
其他一些与您的情况相同的参考资料

What is item in the$scope.selectItems=function(item,city){}您没有在调用item的地方传递它。我认为第一个参数(item)是自动传递给函数的,所以我只需要手动传递第二个参数。这是一个很好的资源:感谢您提供自定义过滤器的教程。但是我不能在ng repeat指令之后使用自定义过滤器,只能在那里使用过滤器。它们的名字令人困惑地相似,但它们是两种不同类型的过滤器。@williamhampshirthank!这似乎是一个很好的解决方案!但这似乎不太管用。因为我只能在ng repeat指令之后使用过滤器,而不能使用自定义过滤器,因为自定义过滤器只能在特定数据之后使用。非常感谢您在plnkr.co上找到我的参考资料!是的,这和我的情况是一样的。它完美地解决了我的问题!多谢各位@杰米
yourmodule.filter('selectItems', function () {
  return function (items, city) {
    //your logic
  };
});


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