推动价值与发展在AngularJS

时间:2015-12-06 00:41:21

标签: javascript angularjs json ionic lodash

我无法将值推送到Ionic / Angular项目中的嵌套数据。

我想在每天的时间(如果有的话)添加一个布尔值,我的json看起来像这样(我正在通过严格的http调用检索json,因此它的树结构必须不要改变。)

{
     "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": []
}
  

我的结果看起来像这样   enter image description here

我的HTML

 <ion-list ng-repeat="(key, value) in filteredDays"
                    ng-model="value.checked" 
                    ng-checked ="value.checked">
          <div class="item item-divider">
               <h3>{{key}}</h3><!--{{value}}--></div>
          <ion-toggle ng-repeat="x in value"
                             ng-model="value" 
                  ng-checked="x" 
                    >
           {{x}}
             </ion-toggle>

我的js

  $scope.filteredDays={};
     $scope.unFilteredDays = {
             "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.filteredDays = $scope.unFilteredDays;

//THIS IS WHERE IM STUCK

/*
  var checked = false;
   $scope.filteredDays.forEach($scope.filteredDays, function(value, key) {
   $scope.filteredDays.push(checked);
 });

* /

我留下了我被困的地方的评论,我似乎无法弄清楚如何将布尔值推送到时间切换项目数组。我设置了codepen here

此外,我已尝试在此项目中使用lodash来处理数据(并获得&#34; all_year&#34;,&#34; season_from&#34;,&#34; season_to&#34;价值不在列表中)并且相信我已经正确设置了它,但是,我对它的经验太缺乏了,无法让它做我想做的事。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

$scope.filteredDays不是数组,因此没有forEach方法。而不是:

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

试试这个:

var checked = false;
Object.keys($scope.filteredDays).forEach(function(key) {
    $scope.filteredDays[key].checked = checked;
});

答案 1 :(得分:0)

var dayNames = ["sunday ", "monday",... Etc];
_.forEach($scope.filteredDays, function(v, day) {
  if (_.contains(dayNames, day)) {
    v.push(checked);
  }
});