我的angularjs应用中有一个复选框作为过滤器:
这是我的数据:
app.controller('listdata', function($scope, $http) {
$scope.users = [{
"name": "pravin",
"queue": [{
"number": "456",
"status": "Unavailable"
}],
"phone": "7411173737"
},
{
"name": "pratik",
"queue": [{
"number": "111",
"status": "Unavailable"
}],
"phone": "8558855858"
},
{
"name": "priyanka",
"queue": [{
"number": "111",
"status": "Unavailable"
}],
"phone": "5454573737"
},
{
"name": "prerana",
"queue": [{
"number": "111",
"status": "Unavailable"
}],
"phone": "7454543737"
}];
});
我在这样的视图中显示上述数据:
//这是过滤器
<label class="switch">
<input ng-true-value='111' ng-false-value='' type="checkbox" ng-model="queue111">111
</label>
// ng重复部分
<div class="row" ng-controller="listdata">
<div ng-repeat="user in users|filter:queue111">
<p> {{user.name}} {{user.phone}}</p>
</div>
</div>
启动应用程序时,将显示用户数据阵列中的所有四个用户。 当我单击过滤器复选框时,它将显示队列中包含数字111的用户。但是由于包含电话号码7411173737(其中包含111),它还会显示用户pravin。我希望此过滤器仅显示其队列编号的记录与111匹配,而不与此处的电话号码等任何其他字段匹配。因此,筛选器应仅显示队列号为111的记录。
是否可以做类似
的操作 <input ng-true-value='111' ng-false-value='' type="checkbox" ng-model="queue111.queue.number">111
当前输出:它显示$ scope.users中的所有四个数据对象
预期输出:仅显示队列编号中的数据对象 $ scope.users中是111
我该怎么做?
答案 0 :(得分:1)
尝试使用此自定义过滤器功能以仅匹配队列数组中的值。
var Controller = function($scope) {
$scope.users = [{
"name": "pravin",
"queue": [{
"number": "456",
"status": "Unavailable"
}],
"phone": "7411173737"
},
{
"name": "pratik",
"queue": [{
"number": "111",
"status": "Unavailable"
}],
"phone": "8558855858"
},
{
"name": "priyanka",
"queue": [{
"number": "111",
"status": "Unavailable"
}],
"phone": "5454573737"
},
{
"name": "prerana",
"queue": [{
"number": "111",
"status": "Unavailable"
}],
"phone": "7454543737"
}
];
$scope.filter = function(item) {
if ($scope.queue111) {
return JSON.stringify(item.queue).includes($scope.queue111);
} else {
return true;
}
};
};
angular
.module('app', [])
.controller('Controller', Controller);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="Controller">
<label class="switch">
<input ng-true-value='111' ng-false-value='' type="checkbox" ng-model="queue111">
</label>
<pre>Search value: {{queue111}}</pre>
<div ng-repeat="user in users | filter:filter">
{{user.name}} {{user.phone}}
</div>
</div>
答案 1 :(得分:1)
我通过在过滤器中使用自定义函数和三元运算符来修复了该问题:
var app = angular.module('myApp', []);
app.controller('listdata', function($scope, $http) {
$scope.users = [{
"name": "pravin",
"queue": [{
"number": "456",
"status": "Unavailable"
}],
"phone": "7411173737"
},
{
"name": "pratik",
"queue": [{
"number": "111",
"status": "Unavailable"
},
{
"number": "112",
"status": "Unavailable"
}],
"phone": "8558855858"
},
{
"name": "priyanka",
"queue": [{
"number": "111",
"status": "Unavailable"
}],
"phone": "5454573737"
},
{
"name": "prerana",
"queue": [{
"number": "111",
"status": "Unavailable"
}],
"phone": "7454543737"
}];
$scope.filter111 = function (user) {
return (user.queue.find(({number}) => number === '111'));
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.0/angular.min.js"></script>
<div ng-app="myApp">
<label class="switch">
<input type="checkbox" ng-model="queue111">111
</label>
<div class="row" ng-controller="listdata">
<div ng-repeat="user in users|filter: queue111? filter111: ''">
<p> {{user.name}} {{user.phone}}</p>
</div>
</div>
</div>
答案 2 :(得分:-1)
您可以将true传递给过滤器以启用严格匹配。
<div ng-repeat="user in users|filter:queue111:true">