Angularjs过滤器是 angularjs非常棒的特性之一。有朝一日,你可能需要使用自定义过滤器,幸运的是,你找到了这篇博文。
下面显示的是自定义过滤器长什么样子(请注意myfilter):
我们的自定义过滤器叫做 "myfilter",它有由 ':'隔开的4个参数.
这是一个将会用到的样本输入:
过滤"555"的处理流程由 "windowScopedFilter"执行,它是过滤器 'myfilter'的第四个参数.
下面我们来实现这些功能 (把logging添加到每个输入参数):
上面的代码大多都log了(译者注:将信息显示到控制台). 实际完成过滤的最重要的一部分是:
"return window[args[4]](input)" 调用第四个参数,它是 'windowScopedFilter'.
这是控制台输出:
"------------------------------------------------- begin dump of custom parameters" custom_filter_function.html:21
"input=" [object Array] custom_filter_function.html:22
"param1(string)=" "param1" custom_filter_function.html:23
"arguments=" 5 custom_filter_function.html:25
"param2(string)=" "param2" custom_filter_function.html:27
"param3(bool)=" true custom_filter_function.html:30
"------------------------------------------------- end dump of custom parameters" custom_filter_function.html:32
"------------------------------------------------- begin dump of custom parameters" custom_filter_function.html:21
"input=" [object Array] custom_filter_function.html:22
"param1(string)=" "param1" custom_filter_function.html:23
"arguments=" 5 custom_filter_function.html:25
"param2(string)=" "param2" custom_filter_function.html:27
"param3(bool)=" true custom_filter_function.html:30
"------------------------------------------------- end dump of custom parameters" custom_filter_function.html:32
"input=" [object Array] custom_filter_function.html:22
"param1(string)=" "param1" custom_filter_function.html:23
"arguments=" 5 custom_filter_function.html:25
"param2(string)=" "param2" custom_filter_function.html:27
"param3(bool)=" true custom_filter_function.html:30
"------------------------------------------------- end dump of custom parameters" custom_filter_function.html:32
"------------------------------------------------- begin dump of custom parameters" custom_filter_function.html:21
"input=" [object Array] custom_filter_function.html:22
"param1(string)=" "param1" custom_filter_function.html:23
"arguments=" 5 custom_filter_function.html:25
"param2(string)=" "param2" custom_filter_function.html:27
"param3(bool)=" true custom_filter_function.html:30
"------------------------------------------------- end dump of custom parameters" custom_filter_function.html:32
完整代码:
NamePhone