如何在控制器中使用过滤器?

本文翻译自: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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值