angularjs - AngularJS - 如何通过 ng-repeat 中的整数数组进行过滤?
问题描述
我有一个 ng-repeat,我用这种方式用一个简单的整数过滤它:
看法:
ng-repeat="t in vm.presencesByPerson | filter: vm.customfilter">
控制器:
在这里,我在下拉列表中有一个观察者,并且基于下拉选择,它传递选择的值以用作过滤器:
this.customfilter = { person: { id: parseInt(filters[0])}};
我这样做是因为presencesByPerson
对象的结构是[{person: {id, name, etc..}]
这可以正常工作并被my ng-repeat
过滤。
现在,id
我不想传递一个 ,而是传递一个id
's 数组,如下所示:
arrayID = [x, y, z]
以便它按数组的每个值过滤我的 ng-repeat!
我需要如何修改代码才能使其正常工作?
我想要一个习惯了的类型脚本解决方案语法!谢谢
解决方案
将您的 customFilter 对象更改为使用arrayID[]
this.customfilter = function(person) {
if (arrayID.indexOf(person.id) != -1)
return true;
};
希望能帮助到你!!
var app = angular.module("myShoppingList", []);
app.controller("myCtrl", function($scope) {
$scope.customfilter = function(person) {
if (arrayID.indexOf(person.id) != -1)
return true;
};
$scope.friends = [{ name: 'John', phone: '555-1276', id: 1 },
{ name: 'Mary', phone: '800-BIG-MARY', id: 2 },
{ name: 'Mike', phone: '555-4321', id: 3 },
{ name: 'Adam', phone: '555-5678', id: 4 },
{ name: 'Julie', phone: '555-8765', id: 5 },
{ name: 'Juliette', phone: '555-5678', id: 6 }
]
arrayID = [1, 3, 6];
});
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="myShoppingList" ng-controller="myCtrl">
<label>Search: <input ng-model="searchText"></label>
<table id="searchTextResults">
<tr>
<th>Name</th>
<th>Phone</th>
</tr>
<tr ng-repeat="friend in friends | filter:customfilter">
<td>{{friend.name}}</td>
<td>{{friend.phone}}</td>
</tr>
</table>
</body>
推荐阅读
- android - 使用 withContext 方法在 kotlin 协程中尝试捕获
- ms-access - 如何在 Microsoft Access 中创建“详细信息”按钮?
- r - 如何在 ggplot2 中没有多余空间的情况下进行绘图?
- openmdao - 从哪里获得有关将新优化器添加到 OpenMDAO 的建议的反馈
- javascript - SAP cloud sdk for javascript multiple and filters 直接附加:导致后端出现“Expression can not convert into ABAP select options”
- laravel - Laravel 主动发货方式
- wiremock - 使用证书的 Wiremock 在 Windows 上不起作用
- sql - 如何根据单个表中的两个列值获取 1 条记录?
- python - 用于具有 DistributionLambda 层的 TFServing 的 TensorFlow 自定义模型签名
- swift - AVCaptureConnection.previewLayer 在委托 AVCaptureMetadataOutputObjectsDelegate 中为零