如何使用angularjs在循环中推送数组值?
在这里,我得到了每个商店的所有价值0到4,但我想要苹果的0,三星的0,micromax的0,诺基亚的3,索尼的4,我需要根据商店推送商品价值,这样任何人都可以帮助如何根据商店推送价值我使用的是ionic frame work可折叠列表,我已经在这里附上了我的代码 角度。模块'ionicApp',['ionic'] .controller'MyCtrl',函数$scope{ var商店=[‘苹果’、‘三星’、‘micromax’、‘诺基亚’、‘索尼’]; $scope.groups=[]; 对于var i=0;i控制器:如何使用angularjs在循环中推送数组值?,angularjs,Angularjs,在这里,我得到了每个商店的所有价值0到4,但我想要苹果的0,三星的0,micromax的0,诺基亚的3,索尼的4,我需要根据商店推送商品价值,这样任何人都可以帮助如何根据商店推送价值我使用的是ionic frame work可折叠列表,我已经在这里附上了我的代码 角度。模块'ionicApp',['ionic'] .controller'MyCtrl',函数$scope{ var商店=[‘苹果’、‘三星’、‘micromax’、‘诺基亚’、‘索尼’]; $scope.groups=[]; 对于
angular
.module('ionicApp', ['ionic'])
.controller('MyCtrl', function ($scope) {
var stores = ['apple', 'samsung', 'micromax', 'nokia', 'sony'];
$scope.groups = [];
for (var i = 0; i < stores.length; i++) {
$scope.groups.push({
name: stores[i],
items: [],
show: false
});
for (var j = 0; j < 5; j++) {
$scope.groups[i].items.push(j);
}
}
/*
* if given group is the selected group, deselect it
* else, select the given group
*/
$scope.toggleGroup = function (group) {
group.show = !group.show;
};
$scope.isGroupShown = function (group) {
return group.show;
};
});
$scope.groups将为您生成以下结果
如果我正确理解您的问题,您不需要第二个for循环 在js文件中,您有
for (var j=0; j<5; j++) {
$scope.groups[i].items.push(j);
}
您没有提供任何有关物品或其价值的信息。因此,我们可以从商店中将其关联起来。
$scope.group[i].items.push(i);