我已经阅读了有关定义过滤器的模块方法:
myApp.filter('filtername', function() {
return function() {
};
});
但是如果我为我的角度应用程序使用一个简单的函数控制器而不是模块呢?
即。我在html顶部的所有内容都是:
<html ng-app ng-controller="MyApp">
然后我用一个简单的控制器包含一个js文件:
function MyApp($scope, $http){
}
但我需要定义一个自定义过滤器 - 在我的特定情况下,我尝试使用Justin Klemm's object ordering filter订购ngRepeat。那么如何定义不使用模块样式语法的过滤器呢?
答案 0 :(得分:1)
<div ng-app="myApp" ng-controller="myCtrl">
<ul>
<li ng-repeat="item in items | filter: myFilter">{{ item }}</li>
</ul>
...
var myApp= angular.module("myApp", []);
myApp.controller('myCtrl', function($scope ) {
function myFilter(item) {
return item == "some condition"
}
})
答案 1 :(得分:1)
如果您想在应用中创建新过滤器,可以定义自定义过滤器,如下所示:
在你的js中
angular.module("yourModule")
.filter("yourFilretName",function(){
return function(value,arg1,arg2 .....){
var result = [];
// filter processing the object that pass the filtering, must be push on the result array
return result;
}
})
模板中的
<your-tag ng-repeat="book in bookList | yourFilretName :arg1:agr2:....:argn>
.....
</your-tag>