为了符合标准,我应该将过滤器功能移动到实际的 angular.filter 吗?
是的,将过滤器用作实用程序的更好方法(例如作为服务、提供者、工厂的单例)。
过滤器不使用$scope它根据输入创建输出,因此您可以编写过滤器,如下所示:
app.filter('myfilter', function() {
return function( items, types) {
var filtered = [];
angular.forEach(items, function(item) {
// <some conditions>
filtered.push(item); // dummy usage, returns the same object
}
});
return filtered;
};
});
取自Fiddle演示
作为参考
如果您将 codeigniter 框架与angular seed一起使用,您可以看到它们使用名为:
filters.js --> custom angular filters
项目结构如下:
app/ --> all of the files to be used in production
css/ --> css files
app.css --> default stylesheet
img/ --> image files
index.html --> app layout file (the main html template file of the app)
index-async.html --> just like index.html, but loads js files asynchronously
js/ --> javascript files
app.js --> application
controllers.js --> application controllers
directives.js --> application directives
filters.js --> custom angular filters
services.js --> custom angular services
lib/ --> angular and 3rd party javascript libraries
angular/
angular.js --> the latest angular js
angular.min.js --> the latest minified angular js
angular-*.js --> angular add-on modules
version.txt --> version number
partials/ --> angular view partials (partial html templates)
partial1.html
partial2.html