使用 TypeScript 创建自定义 Angular 过滤器 [英] Creating a custom Angular filter with TypeScript
问题描述
我正在尝试找出使用 TypeScript 创建自定义角度过滤器的最佳方法.
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;
}
}
... 有效,但看起来很混乱,因为我想将所有过滤器代码分开.所以我想知道如何将过滤器声明为一个单独的文件(例如过滤器/反向过滤器.ts),以便我可以像这样创建它:
... 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.
关于 TS 和 Angular 的文档在地面上似乎很薄,尤其是在涉及过滤器的地方 - 谁能帮忙?
The documentation for TS and Angular together seems pretty thin on the ground, especially where filters are concerned - can anyone help out?
干杯!
推荐答案
函数可以从这样的模块中导出:
Functions can be exported from modules like this:
module moduleName {
export function myFilter()
{
return function(input)
{
// filter stuff here
return result;
}
}
}
然后在模块外:
myModule.filter("filterName", moduleName.myFilter);
然后就可以通过迭代其公共属性来自动注册在 moduleName
模块中定义的所有过滤器.
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屋!