【发布时间】:2014-08-02 01:59:25
【问题描述】:
有一个应用,其中内容的过滤器会根据单击的按钮而变化。对于更多简单的过滤器,我已经能够弄清楚。但是现在我想创建一个过滤器来显示具有特定范围内的值的内容。
这是我正在尝试使用的功能:
$scope.isGood = function(item){
if(!isNan(item.rating)){
return (item.rating>49);
}
return alert("Something broke!!");
//returns true if the item has a high rating (is good, 50+)
//returns false if the item has a poor rating (is bad, 49-)
}
因为过滤器会根据点击导航的哪个部分而改变,并且导航是动态的,所以对于每个导航按钮的点击,我运行一个函数,从其link.filter 部分获取过滤器并将其放入$scope.selectedFilter,在我的ng-repeat 中使用的变量如下:
<tr class="progress" ng-repeat="idea in ideas | filter:selectedFilter">
由于我有一些过滤器,而不是注入我自己的 $filter 并需要将一堆链接在一起,我想写一些适合 $scope.selectedFilter 变量的东西,因为我只想要一个无论如何都要过滤一次。
以下是动态导航设置方式的示例:
$scope.links = [
{'name':'About', //title that will be displayed on the link
'URL': '#/about', //URL/view that the link will lead to/show
'filter':'' //view may simply change so some info is hidden or revealed
},
{'name':'Active',
'URL': '#/active',
'filter': {active: true} //these {active...} buttons are straightforward and work
},
{'name':'Inactive',
'URL': '#/active',
'filter':{active: false}
},
{'name':'Good Rating',
'URL': '#/active',
'filter': 'isGood(item.rating)' //this is the function I want to filter by when clicked
},
{'name':'Bad Rating',
'URL': '#/active',
'filter':'!isGood(item.rating)'
}
];
这是我要过滤的内容:
$scope.ideas = [
{'title':'Title A',
'rating': 80,
active: true
},
{'title':'Title B',
'rating': 55, //this would have a "good rating"
active: false
},
{'title':'Title C',
'rating': 10, //this would have a "bad rating"
active: true
}
];
但我的过滤器无法正常工作,而且我不确定哪里出错了。我对 Angular 很陌生,我是否在某处搞砸了我的语法?我试图复制this answer 的编写方式,但没有运气。
任何帮助将不胜感激。谢谢!
【问题讨论】:
标签: javascript angularjs filter angularjs-filter