AngularJS 根据数组的值过滤字段
AngularJS filter field according to values of an array
我有一个数组,这个数组包含一些 ID。
我想根据 Id 过滤我的结果。
例如,请参阅下面的代码片段。
如何显示 'StatusId' 位于 'ids' 数组中的记录?
根据我的示例,我只想列出:
AAA
BBB
CCC
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script>
angular.module('myApp', []).controller('ctrl', function ($scope) {
$scope.ids = [{
"id": 1
}, {
"id": 2
}, {
"id": 3
}]
$scope.records = [
{
"Name": "AAA",
"StatusId": 1
},
{
"Name": "BBB",
"StatusId": 1
},
{
"Name": "CCC",
"StatusId": 2
},
{
"Name": "DDD",
"StatusId": 4
},
{
"Name": "EEE",
"StatusId": 4
},
]
});
</script>
<body>
<div id="idctrl" ng-app="myApp" ng-controller="ctrl">
<ul>
<li ng-repeat="x in records">
{{ x.Name }}
</li>
</ul>
</div>
</body>
</html>
您可以使用javascript'filter'函数:
records = $scope.records.filter((record) => {
return record.StatusId === 1
});
这将return一个只包含 StatusId 为 1 的记录的新数组。您可以使用这个更新后的数组来显示您想要的记录。
我有一个数组,这个数组包含一些 ID。 我想根据 Id 过滤我的结果。
例如,请参阅下面的代码片段。 如何显示 'StatusId' 位于 'ids' 数组中的记录? 根据我的示例,我只想列出:
AAA BBB CCC
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script>
angular.module('myApp', []).controller('ctrl', function ($scope) {
$scope.ids = [{
"id": 1
}, {
"id": 2
}, {
"id": 3
}]
$scope.records = [
{
"Name": "AAA",
"StatusId": 1
},
{
"Name": "BBB",
"StatusId": 1
},
{
"Name": "CCC",
"StatusId": 2
},
{
"Name": "DDD",
"StatusId": 4
},
{
"Name": "EEE",
"StatusId": 4
},
]
});
</script>
<body>
<div id="idctrl" ng-app="myApp" ng-controller="ctrl">
<ul>
<li ng-repeat="x in records">
{{ x.Name }}
</li>
</ul>
</div>
</body>
</html>
您可以使用javascript'filter'函数:
records = $scope.records.filter((record) => {
return record.StatusId === 1
});
这将return一个只包含 StatusId 为 1 的记录的新数组。您可以使用这个更新后的数组来显示您想要的记录。