这是我添加到 AngularJS 的过滤器:
angular.module('myApp', [])
.filter('clean', function(){
return function(input){
return input;
};
})
谁能像我五岁一样解释为什么需要额外的匿名函数才能返回数据?
基本上为什么这不起作用:
angular.module('myApp', [])
.filter('clean', function(input){
return input;
})
我正在努力更好地了解这里发生的事情,因此我们将不胜感激。
最佳答案
他们可以用另一种方式来做!但这就是框架,标准化。 查看它以相同方式工作的服务定义。
但是如果您仔细查看文档,它会说 .filter
函数和其他类似的函数应该得到 providers 而不是 values。这有助于:
- 延迟或不实例化。
- DI 和所有 DI 的好处,它允许您在每个过滤器的基础上定义依赖关系。
检查完整的 fiddle http://jsfiddle.net/vAHbr/4/
angular.module('myApp', [])
.filter('discountcurrency',
// The provider, where we can encapsulate our filter
// creation code, without having to do JavaScript ninja
// stuff. As well as this is our chance to ask for
// dependencies.
function ($filter) {
var currency = $filter('currency');
// The actual simple filter function.
return function (value) {
return currency(value - 5);
}
});
关于javascript - AngularJS 过滤器功能解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17909981/