如何使用angularjs在循环中推送数组值?

时间:2015-08-21 04:24:36

标签: angularjs

这里我得到的每个商店都有0到4的所有价值但是我想要0用于苹果1用于三星2用于micromax 3用于诺基亚和4用于索尼我需要根据商店推送项目价值所以任何人都可以帮助如何基于这种方法中的商店推送价值我使用离子框架工作可折叠列表,我已经在这里附上我的代码

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[i] = {
       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;
  };
  
});
.list .item.item-accordion {
  line-height: 38px;
  padding-top: 0;
  padding-bottom: 0;
  transition: 0.09s all linear;
}
.list .item.item-accordion.ng-hide {
  line-height: 0px;
}
.list .item.item-accordion.ng-hide-add,
.list .item.item-accordion.ng-hide-remove {
  display: block !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="ionicApp">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>Ionic collapsible list</title>
   
    <link href="//code.ionicframework.com/1.0.0-beta.12/css/ionic.css" rel="stylesheet">
    <script src="//code.ionicframework.com/1.0.0-beta.12/js/ionic.bundle.js"></script>
    </script>
  </head>

  <body ng-controller="MyCtrl">
    
    <ion-header-bar class="bar-positive">
      <h1 class="title">Ionic collapsible list</h1>
    </ion-header-bar>

    <ion-content>

      <ion-list>
        <div ng-repeat="group in groups">
          <ion-item class="item-stable"
                    ng-click="toggleGroup(group)"
                    ng-class="{active: isGroupShown(group)}">
              <i class="icon" ng-class="isGroupShown(group) ? 'ion-minus' : 'ion-plus'"></i>
            &nbsp;
            {{group.name}}
          </ion-item>
          <ion-item class="item-accordion"
                    ng-repeat="item in group.items"
                    ng-show="isGroupShown(group)">
            {{item}}
          </ion-item>
        </div>
      </ion-list>

    </ion-content>
      
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

控制器:

 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将导致您如下所示 enter image description here

答案 1 :(得分:0)

如果我理解你的问题是正确的,那么你不需要第二个for循环。

在你的js文件中,你有

for (var j=0; j<5; j++) {
  $scope.groups[i].items.push(j);
}

我认为你需要的只是

$scope.group[i].items.push(i);