sách gpt4 ai đã đi

javascript - 当我使用过滤器时如何迭代我的矩阵

In lại 作者:行者123 更新时间:2023-12-03 07:20:08 27 4
mua khóa gpt4 Nike

大家好,我正在使用 ArgularJs。我需要解决这个问题,当我使用过滤器时,我无法迭代我的矩阵以将其值显示到我的表中。






{{ item.id }}
{{ item.name }}
{{ item.price }}


这是我的脚本 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';
});

当我运行时出现此错误:

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 显示为我的选择中的默认选择。

感谢您的帮助

1 Câu trả lời

charlietfl 正确地指出 $scope.selectedCity 是一个字符串,不能与 ng-repeat 一起使用,您需要给它一个数组。

我认为你想做的是:


更新了jsfiddle:https://jsfiddle.net/bgg5yfot/1/

关于javascript - 当我使用过滤器时如何迭代我的矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36242995/

27 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com