gpt4 book ai didi

javascript - 单个值的 Angular JS 过滤器

转载 作者:行者123 更新时间:2023-11-29 15:32:55 27 4
gpt4 key购买 nike

这是我尝试过的:

<section data-ng-controller="HomeController">
<table ng-repeat="item in servers | unique:'_id.idc'">
<tr>
<td> {{ item._id.idc }} </td>
</tr>
<tr ng-repeat="data in servers | filter:{_id.idc: 'LH5'}: true ">
<td>{{data}}</td>
</tr>
</table>

这是当前输出:

LH8
LH5
AMS

如果我删除这里的过滤器是来自{{数据}}的一些样本

{"_id":{"cluster":0,"idc":"LH8"},"Physical":{"SumCores":488,"SumMemory":3232},"Virtual":{"SumCores":2,"SumMemory":8}}
{"_id":{"cluster":1,"idc":"LH8"},"Physical":{"SumCores":256,"SumMemory":1024},"Virtual":{"SumCores":232,"SumMemory":469}}

为什么过滤不正确? unique 工作得很好,但单个过滤器没有。

编辑:我还检查了唯一过滤器是否与它有某种冲突,但如果没有该过滤器,它仍然无法工作。

最佳答案

您的过滤器对于嵌套对象的格式不正确。应该是:

ng-repeat="data in servers | filter: { _id: { idc: 'LH5' } }"

下面是一个有效的简化示例。

var myApp = angular.module('MyApp', []);

myApp.controller('MyController', ['$scope', function($scope) {
$scope.filter = 'red';
$scope.cars = [
{
make: 'Ford',
model: 'Focus',
year: '2012',
colors: {
interior: 'red',
exterior: 'blue'
}
},
{
make: 'Ford',
model: 'Fusion',
year: '2009',
colors: {
interior: 'green',
exterior: 'black'
}
},
{
make: 'Honda',
model: 'Civic',
year: '2011',
colors: {
interior: 'red',
exterior: 'silver'
}
}
]
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>

<div ng-app="MyApp" ng-controller="MyController">
<label for="filter">Interior color filter: </label>
<input type="text" ng-model="filter" id="filter" />
<div ng-repeat="car in cars | filter: { colors: { interior: filter } }">{{ car.year }} {{ car.make }} {{ car.model }}</div>
</div>

关于javascript - 单个值的 Angular JS 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32633748/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com