用TypeScript创建一个自定义的Angular过滤器 [英] Creating a custom Angular filter with TypeScript

查看:294
本文介绍了用TypeScript创建一个自定义的Angular过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我看到的所有代码示例都是这样使用的:

>

  myModule.filter(myFilter,function()
{
返回函数(输入)
{
//过滤这里的东西
返回结果;
}
}

...这工作,但似乎混乱,因为我想保持我所有的过滤代码分开。所以我想知道如何声明过滤器作为一个单独的文件(例如filters / reverse-filter.ts)所以我可以像这样创建它:

$ $ $ $ $ $ $ $ myModule.filter(filterName,moduleName.myFilter);
code>

...与控制器,服务等相同的方式。

< TS和Angular在一起的文档似乎很薄弱,特别是在过滤器方面 - 任何人都可以帮忙吗?

干杯!

解决方案

<可以从这样的模块中导出函数:

pre $ module $ module $ module $ {
export function myFilter()
{
返回函数(输入)
{
//过滤这里的东西
返回结果;



$ / code $ / pre

然后在模块之外: / p>

  myModule.filter(filterName,moduleName.myFilter); 

然后就可以做一些事情,比如自动注册 moduleName 模块迭代其公共属性。


I'm trying to work out the best way of creating a custom Angular Filter with TypeScript.

All the code samples I see use something like:

myModule.filter( "myFilter", function()
{
    return function( input )
    {
        //  filter stuff here
        return result;
    }
}

... which works, but seems messy as I want to keep all my filter code separate. So I want to know how to declare the filter as a separate file (eg filters/reverse-filter.ts) so I can create it like:

myModule.filter( "filterName", moduleName.myFilter );

... the same way you would for Controllers, Services etc.

The documentation for TS and Angular together seems pretty thin on the ground, especially where filters are concerned - can anyone help out?

Cheers!

解决方案

Functions can be exported from modules like this:

module moduleName {
    export function myFilter()
    {
        return function(input)
        {
            //  filter stuff here
            return result;
        }
    }
}

then outside the module:

myModule.filter("filterName", moduleName.myFilter);

Then it would then be possible to do things like automatically register all of the filters defined in the moduleName module by iterating over its public properties.

这篇关于用TypeScript创建一个自定义的Angular过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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