如何遍历我矩阵,当我使用的过滤器 [英] how to iterate my matrix when I use a filter
本文介绍了如何遍历我矩阵,当我使用的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好我与ArgularJs工作。我要解决这个问题,当我使用的过滤器,我不能重复我的矩阵,以显示其值到我的表。
Hello everyone I am working with ArgularJs. I need to fix this problem, when I use a filter, I can't iterate my matrix to show its values into my table.
<div ng-app="app" ng-controller="ctrl">
<select ng-model="selectedCity" ng-options="x as (x | cityFilter) for (x, y) in cities">
</select>
</div>
<table>
<tr ng-repeat="item in selectedCity">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.price }}</td>
</tr>
</table>
这是我的脚本JS:
and this is my script js:
angular.module('app', [])
.filter('cityFilter', function() {
return function(city) {
return city.replace('_', ' ');
}
})
.controller('ctrl', function($scope) {
$scope.cities = {
city: [{
id: 'c01',
name: 'name1',
price: 15
}, {
id: 'c02',
name: 'name2',
price: 18
}, {
id: 'c03',
name: 'name3',
price: 11
}],
city_02: [{
id: 'cc01',
name: 'name11',
price: 10
}, {
id: 'cc02',
name: 'name22',
price: 14
}, {
id: 'cc03',
name: 'name33',
price: 11
}],
city_03: [{
id: 'ccc01',
name: 'name111',
price: 19
}, {
id: 'ccc02',
name: 'name222',
price: 18
}, {
id: 'ccc03',
name: 'name333',
price: 10
}]
};
$scope.selectedCity = 'city_02';
});
当我运行我得到这个错误:
when I run I get this error :
Error: ngRepeat:dupes
Duplicate Key in Repeater
Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: item in selectedCity, Duplicate key: string:a, Duplicate value: a
我想展示城市02在我选择一个默认的选择。
I would like to show city 02 as a default choice in my select.
感谢您的帮助。
推荐答案
charlietfl是正确地指出, $ scope.selectedCity
是一个字符串,不能与使用 NG-重复
,你需要给它一个数组。
charlietfl is right in pointing out that $scope.selectedCity
is a string and cannot be used with ng-repeat
, you need to give it an array.
我认为你正在尝试做的是:
I think what you are trying to do is:
<tr ng-repeat="item in cities[selectedCity]">
更新的jsfiddle: https://jsfiddle.net/bgg5yfot/1/
这篇关于如何遍历我矩阵,当我使用的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文