(Angular) 怎样(或能否)在 filter 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;
    };
  });

上述代码不起作用。因为在filter filter 筛选函数 selectItems 中有第二个参数city。那么在这种情况下,该怎样以正式的方式在筛选函数中使用第二个参数呢?或者说,Angular是否允许在筛选函数中添加第二个参数?

阅读 2.8k
1 个回答

返回一个函数:

angular.module("myApp", [])
  .controller("MyCtrl", function ($scope) {
    $scope.selectItems = function (city) {
      return function(item) {
        return item.city === city;
      }
    };
  });
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题