如何在嵌套的ng-repeat中检索动态数组数据?

时间:2015-12-05 01:05:28

标签: arrays angularjs json nested angularjs-ng-repeat

我正在处理json数据,这些数据与字符串和数组非常嵌套,我试图在每天推送truefalse值,我这样做了

没有嵌套数组(codepen 1)

HTML

  <ion-toggle ng-repeat="item in product"
                    ng-model="item.checked" 
                    ng-checked="item.checked">
          {{item}} 
        </ion-toggle> 

JS

 $scope.eventDaysList = [
{ day: "Monday" },
{ day: "Tuesday"},
{ day: "Wednesday" },
 ....
  ];


$scope.product = $scope.eventDaysList;
    var checked = false;
    $scope.product.forEach(function (newCheckItem) {
    newCheckItem.checked = checked;
});
  

enter image description here

作品:)很酷。

然而,当值(小时)嵌套时,我很难过。

使用嵌套数组(codepen 2)

嵌套小时看起来像这个

$scope.product.value.forEach(function (newCheckItem) { 

<ion-toggle 
                  ng-repeat="(key,value) in product" 
                  ng-model="value.checked" 
                  ng-checked="value.checked" 
                   ng-hide="key == all_year, season_from, season_to">
        {{ key }} at {{ value }} 
      </ion-toggle>

JS

    $scope.eventDaysList = {
    "event_offering": {
      "all_year": true,
      "season_from": "01/01",
      "season_to": "12/31",
      "monday": [
        "08:30am"
      ],
      "tuesday": [
        "08:30am"
      ],
      "wednesday": [
        "08:00am", "09:30am", "01:30pm"
      ],
      "thursday": [
        "08:30am", "09:30am"
      ],
      "friday": [
        "08:30am"
      ],
      "saturday": [],
      "sunday": []
    }
   };

       $scope.product= $scope.eventDaysList.event_offering
       var checked = false;
        //   $scope.product.forEach(function (newCheckItem) {
        //$scope.product.value.forEach(function (newCheckItem) {
         //   newCheckItem.checked = checked;
         //   });*/

enter image description here

所以我的问题是:

如何将动态数组中的嵌套值检索为单独的对象,即将weds切换为三个切换,同时仍保留结构的其余部分?如何在ng-repeat中隐藏我的三个静态键all_year, season_from, season_to

1 个答案:

答案 0 :(得分:1)

ng-repeat并不打算进行嵌套集合。我建议你找出一种方法来展平你的event_offering。你可以做这个客户端,但我建议扁平化这个呼叫服务器端。

我确实解决了你的问题客户端,你可以查看这个codepen:

http://codepen.io/anon/pen/eJYVzx

我精简了代码,专注于你正在努力完成的事情。我还将lodash添加到速记foreach循环中。我希望这会有所帮助。

JS:

  $scope.events = [];


$scope.eventDaysList = {
    'eventOfferingRevised' : [
      {'name': 'all_year', 'checked': true}
      ,{'name': 'monday', 'time': [{'name': '8:30', 'checked': true},{'name': '9:30', 'checked': true}] }
    ]
  };

  $scope.products = $scope.events;


_.each($scope.eventDaysList.eventOfferingRevised, function(eventOffering){
  if(eventOffering.time){
    _.each(eventOffering.time, function(eventTime) {
      eventTime.name = eventOffering.name + '-' + eventTime.name;
      $scope.events.push(eventTime);
    });
  } else {
    $scope.events.push(eventOffering);
  }
});

HTML:

<ion-toggle 
              ng-repeat="product in products" 
              ng-model="product.checked" 
              ng-checked="product.checked" 
               ng-hide="key == all_year, season_from, season_to">
    {{product.name}}
  </ion-toggle>