我已经写了一个过滤器函数,它将根据您传递的参数返回数据。我希望在控制器中有相同的功能。是否有可能在控制器中重用过滤器函数?

这是我目前为止尝试过的:

function myCtrl($scope,filter1)
{ 
    // i simply used the filter function name, it is not working.
}

当前回答

似乎没有人提到你可以在$filter('filtername')(arg1,arg2)中使用函数arg2;

例如:

$scope.filteredItems = $filter('filter')(items, function(item){return item.Price>50;});

其他回答

注入$filter到你的控制器

function myCtrl($scope, $filter)
{
}

然后无论你想在哪里使用这个过滤器,就像这样使用它:

$filter('filtername');

如果你想把参数传递给这个过滤器,使用单独的括号:

function myCtrl($scope, $filter)
{
    $filter('filtername')(arg1,arg2);
}

其中arg1是要筛选的数组,arg2是用于筛选的对象。

@Prashanth提供的答案是正确的,但还有更简单的方法来做同样的事情。基本上,不用注入$filter依赖项并使用调用它的笨拙语法($filter('filtername')(arg1,arg2);),而是可以注入依赖项:过滤器名称加上filter后缀。

以问题为例,你可以这样写:

function myCtrl($scope, filter1Filter) { 
  filter1Filter(input, arg1);
}

需要注意的是,无论你使用什么命名约定,你都必须将Filter附加到过滤器名称后: foo通过调用fooFilter来引用 通过调用fooFilterFilter来引用fooFilter

使用下面的示例代码,我们可以通过名称过滤angular控制器中的数组。这是基于下面的描述。 http://docs.angularjs.org/guide/filter

这一点。filteredArray = filterFilter数组,{名称:“伊戈尔”});

JS:

 angular.module('FilterInControllerModule', []).
    controller('FilterController', ['filterFilter', function(filterFilter) {
      this.array = [
        {name: 'Tobias'},
        {name: 'Jeff'},
        {name: 'Brian'},
        {name: 'Igor'},
        {name: 'James'},
        {name: 'Brad'}
      ];
      this.filteredArray = filterFilter(this.array, {name:'Igor'});
    }]);

HTML

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example96-production</title>
  

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.3/angular.min.js"></script>
  <script src="script.js"></script>
  

  
</head>
<body ng-app="FilterInControllerModule">
    <div ng-controller="FilterController as ctrl">
    <div>
      All entries:
      <span ng-repeat="entry in ctrl.array">{{entry.name}} </span>
    </div>
    <div>
      Filter By Name in angular controller
      <span ng-repeat="entry in ctrl.filteredArray">{{entry.name}} </span>
    </div>
  </div>
</body>
</html>

下面是在Angular控制器中使用过滤器的另一个例子:

$scope.ListOfPeople = [
    { PersonID: 10, FirstName: "John", LastName: "Smith", Sex: "Male" },
    { PersonID: 11, FirstName: "James", LastName: "Last", Sex: "Male" },
    { PersonID: 12, FirstName: "Mary", LastName: "Heart", Sex: "Female" },
    { PersonID: 13, FirstName: "Sandra", LastName: "Goldsmith", Sex: "Female" },
    { PersonID: 14, FirstName: "Shaun", LastName: "Sheep", Sex: "Male" },
    { PersonID: 15, FirstName: "Nicola", LastName: "Smith", Sex: "Male" }
];

$scope.ListOfWomen = $scope.ListOfPeople.filter(function (person) {
    return (person.Sex == "Female");
});

//  This will display "There are 2 women in our list."
prompt("", "There are " + $scope.ListOfWomen.length + " women in our list.");

很简单吧?

还有另一种方法来计算从视图中镜像语法的过滤器。调用是复杂的,但您可以构建到它的快捷方式。我喜欢这个字符串的语法和视图中的是一样的。看起来是这样的:

function myCtrl($scope, $interpolate) { 
  $scope.$eval($interpolate( "{{ myvar * 10 | currency }} dollars." ))
}