javascript - (Angular) 怎样(或能否)在 filter filter 筛选函数中使用第二个参数?

查看:102
本文介绍了javascript - (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是否允许在筛选函数中添加第二个参数?

解决方案

返回一个函数:

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

这篇关于javascript - (Angular) 怎样(或能否)在 filter filter 筛选函数中使用第二个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆