Javascript AngularJs过滤器带有;或;条件
代码在此处可用:。过滤器Javascript AngularJs过滤器带有;或;条件,javascript,angularjs,Javascript,Angularjs,代码在此处可用:。过滤器过滤器:{a:'x'}工作,只显示一行 问题: 似乎filter:{a:'xxx'}匹配a中的任何文本,只要文本包含字符串xxx。如果我想做精确匹配呢 如何实现:如果a='xxx'或b=3未使用c,则显示所有行 也许在'model.dashbardRows'上使用javascript代码更好 dashboard.component.js (function () { 'use strict'; angular.module('testModule', []
过滤器:{a:'x'}
工作,只显示一行
问题:
filter:{a:'xxx'}
匹配a
中的任何文本,只要文本包含字符串xxx
。如果我想做精确匹配呢a='xxx'或b=3
未使用c
,则显示所有行(function () {
'use strict';
angular.module('testModule', [])
.component('dashboard', {
templateUrl: 'dashboard.component.html',
controllerAs: 'model',
controller: [controller]
});
function controller() {
var model = this;
model.dashboardRows = [
{a:'xxx', b:1, c:'ss'},
{a:'yyy', b:2, c:'tt'},
{a:'zzz', b:3, c:'uu'}];
}
})();
dashboard.component.html
<div>
Test
<table>
<tr ng-repeat="i in model.dashboardRows | filter: { a : 'x' }">
<td>{{i.a}}</td>
<td>{{i.b}}</td>
</tr>
</table>
</div>
试验
{{i.a}}
{{i.b}}
查看文档,您可以传入可选的比较器
参数
true
:函数(实际,预期){return angular.equals(实际,预期)}的缩写。这本质上是对预期和实际的严格比较
注意,这是区分大小写的(因为字符串比较在JS中是区分大小写的)
在页面底部还有一个示例,演示了如何过滤所有或仅过滤单个属性。如果您的筛选很复杂,或者您的源数据很大,那么在controller中进行筛选也有助于提高可读性和性能。查看文档,您可以传入可选的比较器
参数
true
:函数(实际,预期){return angular.equals(实际,预期)}的缩写。这本质上是对预期和实际的严格比较
注意,这是区分大小写的(因为字符串比较在JS中是区分大小写的)
在页面底部还有一个示例,演示了如何过滤所有或仅过滤单个属性。如果您的筛选很复杂,或者您的源数据很大,那么控制器中的筛选也可以帮助提高可读性和性能。我认为您应该使用自定义筛选
module.filter('myFilter', function(){
return function(input){
return input.filter(yourFilterFn);
};
});
ng-repeat="item in items | myFilter"
我认为你应该使用自定义过滤器
module.filter('myFilter', function(){
return function(input){
return input.filter(yourFilterFn);
};
});
ng-repeat="item in items | myFilter"
正如@Valery所建议的,使用自定义过滤器是解决这一问题的最佳解决方案 dashboard.component.js
.filter("optionalFilter",function(){
//console.log("filter loads");
return function(items, firstArgument,secondArgument){
//console.log("item is ",items); // it is value upon which you have to filter
//console.log("firstArgument is ",firstArgument);
//console.log("secondArgument ",secondArgument);
var filtered = [];
angular.forEach(items, function(value, key) {
//console.log("val ::",value);
if(value.a == firstArgument || value.b == secondArgument){
// console.log("val ::",value.a);
this.push(value);
}
}, filtered);
//console.log("val ::",filtered);
return filtered;
}
dashboard.component.html
<tr ng-repeat="i in model.dashboardRows | optionalFilter:'aaa':2">
有用的参考资料:
.filter("optionalFilter",function(){
//console.log("filter loads");
return function(items, firstArgument,secondArgument){
//console.log("item is ",items); // it is value upon which you have to filter
//console.log("firstArgument is ",firstArgument);
//console.log("secondArgument ",secondArgument);
var filtered = [];
angular.forEach(items, function(value, key) {
//console.log("val ::",value);
if(value.a == firstArgument || value.b == secondArgument){
// console.log("val ::",value.a);
this.push(value);
}
}, filtered);
//console.log("val ::",filtered);
return filtered;
}
dashboard.component.html
<tr ng-repeat="i in model.dashboardRows | optionalFilter:'aaa':2">
有用的参考资料:
c
,我不希望在比较时使用c
。我更新了问题并在列表中添加了另一个属性c
,我不希望在比较时使用c
。