本文翻译自:How to use a filter in a controller?
I have written a filter function which will return data based on the argument you are passing. 我编写了一个过滤函数,它将根据您传递的参数返回数据。 I want the same functionality in my controller. 我想在我的控制器中使用相同的功能。 Is it possible to reuse the filter function in a controller? 是否可以在控制器中重用过滤器功能?
This is what I've tried so far: 这是我到目前为止所尝试的:
function myCtrl($scope,filter1)
{
// i simply used the filter function name, it is not working.
}
#1楼
参考:https://stackoom.com/question/y0fj/如何在控制器中使用过滤器
#2楼
Inject $filter to your controller 向控制器注入$ filter
function myCtrl($scope, $filter)
{
}
Then wherever you want to use that filter, just use it like this: 然后,无论您想要使用哪个过滤器,只需使用它:
$filter('filtername');
If you want to pass arguments to that filter, do it using separate parentheses: 如果要将参数传递给该过滤器,请使用单独的括号:
function myCtrl($scope, $filter)
{
$filter('filtername')(arg1,arg2);
}
Where arg1
is the array you want to filter on and arg2
is the object used to filter. 其中arg1
是要过滤的数组, arg2
是用于过滤的对象。
#3楼
Answer provided by @Prashanth is correct, but there is even easier way of doing the same. @Prashanth提供的答案是正确的,但有更简单的方法可以做到这一点。 Basically instead of injecting the $filter
dependency and using awkward syntax of invoking it ( $filter('filtername')(arg1,arg2);
) one can inject dependency being: filter name plus the Filter
suffix. 基本上不是注入$filter
依赖项并使用调用它的笨拙语法( $filter('filtername')(arg1,arg2);
)而是可以注入依赖项:filter name加上Filter
后缀。
Taking example from the question one could write: 从一个问题的例子可以写:
function myCtrl($scope, filter1Filter) {
filter1Filter(input, arg1);
}
It should be noted that you must append Filter
to the filter name, no matter what naming convention you're using: foo is referenced by calling fooFilter
应该注意的是,无论您使用什么命名约定,都必须将Filter
附加到过滤器名称:通过调用fooFilter
引用foo
fooFilter is referenced by calling fooFilterFilter
通过调用fooFilterFilter
引用fooFilterFilter
#4楼
I have another example, that I made for my process: 我有另一个例子,我为我的过程做了:
I get an Array with value-Description like this 我得到一个像这样的值 - 描述的数组
states = [{
status: '1',
desc: '\u2713'
}, {
status: '2',
desc: '\u271B'
}]
in my Filters.js: 在我的Filters.js中:
.filter('getState', function () {
return function (input, states) {
//console.log(states);
for (var i = 0; i < states.length; i++) {
//console.log(states[i]);
if (states[i].status == input) {
return states[i].desc;
}
}
return '\u2718';
};
})
Then, a test var (controller): 然后,测试var(控制器):
function myCtrl($scope, $filter) {
// ....
var resp = $filter('getState')('1', states);
// ....
}
#5楼
There is another way to evaluate filters that mirrors the syntax from the views. 还有另一种方法可以评估从视图中反映语法的过滤器。 The invocation is hairy but you could build a shortcut to it. 调用是毛茸茸的,但你可以建立一个快捷方式。 I like that the syntax of the string is identical to what you'd have in a view. 我喜欢字符串的语法与您在视图中的语法相同。 Looks like this: 看起来像这样:
function myCtrl($scope, $interpolate) {
$scope.$eval($interpolate( "{{ myvar * 10 | currency }} dollars." ))
}
#6楼
Using following sample code we can filter array in angular controller by name. 使用以下示例代码,我们可以按名称过滤角度控制器中的数组。 this is based on following description. 这基于以下描述。 http://docs.angularjs.org/guide/filter http://docs.angularjs.org/guide/filter
this.filteredArray = filterFilter(this.array, {name:'Igor'}); this.filteredArray = filterFilter(this.array,{name:'Igor'});
JS: 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 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>