如何在控制器中使用过滤器? [英] How to use a filter in a controller?

查看:29
本文介绍了如何在控制器中使用过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个过滤器函数,它将根据您传递的参数返回数据.我想在我的控制器中使用相同的功能.是否可以在控制器中重用过滤器功能?

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.
}

推荐答案

Inject $filter 到你的控制器

Inject $filter to your controller

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);
}

其中 arg1 是要过滤的数组,arg2 是用于过滤的对象.

Where arg1 is the array you want to filter on and arg2 is the object used to filter.

这篇关于如何在控制器中使用过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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