我的代码正在输出:
- 红色
- 绿色
使用 $scope.selected = '123'
的模型,我如何将其编辑为仅输出:
- 红色
这是我的观点:
<body ng-controller="MainCtrl">
<ul>
<li ng-repeat="item in items">
{{item.color}}
</li>
</ul>
</body>
这是我的 Controller :
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.selected = '123';
$scope.items = {
'123': {
color: 'red',
quantity: 3
},
'456': {
color: 'blue',
quantity: 7
}
};
});
我尝试使用带有 selected
的过滤器,但没有成功。
最佳答案
通过将项目更改为数组:
$scope.items =[
{
id:'123',
color: 'red',
quantity: 3
},
{
id:'456',
color: 'blue',
quantity: 7
}
];
您可以使用内置的filter
,它只适用于数组(也有人谈论对象过滤,不确定它是否存在)
<li ng-repeat="item in items | filter: {id:selected}">
{{item.color}}
</li>
一般来说,使用数组数据比使用对象更容易进行排序、过滤、索引等操作会更好
关于javascript - AngularJS 过滤器对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31436359/