不需要的角度值链接

时间:2016-11-24 09:39:52

标签: javascript angularjs

图片显示了一个编辑页面,可以使用该命令向列表中添加命令,删除等等。用户单击“更新”按钮时,该值应仅更新为实际数组。以下是我的一些代码:

  $scope.editSchedule = function(index){
              console.log(index);
      $scope.editScheduleValue = {
        name: $scope.currentSchedule[index].name,
        trigger: $scope.currentSchedule[index].trigger,
        repeat: $scope.currentSchedule[index].repeat,
        commandList: $scope.currentSchedule[index].commandList,
        scheduleIndex: index
      };
      var dailog = $uibModal.open({
          templateUrl: 'app/partials/edit-schedule.html',
          controller: editScheduleController,
          size: 'lg',
          scope: $scope
      });
    };

这是一个编辑按钮范围,它将从curremtSchedule数组中获取实际值。

$scope.addCommand = function(){
    console.log("addCommand");
    $scope.addRoom = $scope.equipment[$scope.roomSelected].name;
    $scope.addEquipment = $scope.equipment[$scope.roomSelected].equipment[$scope.equipmentSelected].name;
    $scope.addEquipmentCommand = $scope.equipment[$scope.roomSelected].equipment[$scope.equipmentSelected].command[$scope.commandSelected].type;
    $scope.editScheduleValue.commandList.push({
      room: $scope.addRoom,
      equipment: $scope.addEquipment,
      command: $scope.addEquipmentCommand
    })
  };

这是我的添加命令按钮代码,它将数据推送到editScheduleValue数组。

HTML:

<tr ng-repeat="x in editScheduleValue.commandList" ui-tree-node>
        <td style="width: 5%"><i class="glyphicon glyphicon-resize-vertical" ui-tree-handle></i> </td>
        <td style="width: 5%">{{$index+1}}</td>
        <td style="width: 30%">{{x.room}}</td>
        <td style="width: 30%">{{x.equipment}}</td>
        <td style="width: 30%">{{x.command}}</td>
        <td>
        <a class="pull-right btn btn-danger btn-xs" data-nodrag ng-click="remove(this)">
          <span class="glyphicon glyphicon-remove"></span>
        </a>
      </td>
      </tr>

我修改的问题是每当我删除时,添加命令不仅更新了editScheduleValue数组,而且还添加了currentSchedule数组,我真的不明白为什么这个2数组是以某种方式链接的。请帮帮忙~~~ 谢谢。

1 个答案:

答案 0 :(得分:0)

我替换

commandList: $scope.currentSchedule[index].commandList,

使用

commandList: angular.copy($scope.currentSchedule[index].commandList),

并且这2个数组不再链接了,我不太明白为什么会这样,但这里是我的问题的答案。