javascript - 为什么 Angular 范围保持引用?

标签 javascript angularjs scope

JS

angular.module('bindExample', []).controller('ExampleController', ['$scope', function($scope) {
    $scope.gridFields = {
        id: {
            width: 50
        },
        price: {
            width: 60
        },
    };
    $scope.allData = {
        'one': {
            id: '1234qwe',
            price: 900
        },
        'two': {
            id: 'asdadw',
            price: 1700
        },
        'three': {
            id: '342sdaw',
            price: 1200
        },
    };
    $scope.edit = function(row) {
        console.log(row);
        $scope.buffer = $scope.allData[row];
    }
}]);

HTML

<div ng-app="bindExample">
   <div ng-controller="ExampleController">
      <table>
         <tbody>
            <tr ng-repeat="(row, data) in allData">
               <td ng-repeat="(field, option) in gridFields" ng-bind="data[field]"></td>
               <td><button ng-click="edit(row)">edit</button></td>
            </tr>
         </tbody>
      </table>
      <div>
         <input type='text' ng-model="buffer.id"/>
      </div>
      <div>
         <input type='text' ng-model="buffer.price"/>
      </div>
   </div>
</div>

单击编辑后,值从 $scope.allData 转到 $scope.buffer 变量,并且输入使用缓冲区作为模型,但是当输入更改时,allData 变量中的值也会更改,但我不这样做想要这个,这就是为什么尝试将值传递给其他...

此处说明的问题:JSFIDDLE

有什么想法吗?

最佳答案

使用angular.copy()

    $scope.buffer = angular.copy($scope.allData[row]);

关于javascript - 为什么 Angular 范围保持引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32598454/

相关文章:

页面最小化/离开后的Javascript setInterval "catches up"

javascript - 下拉菜单显示在右侧而不是 anchor 下方

angular - 通过 Angular2 的所有组件访问变量

c++ - 函数参数的后期销毁

PHP 范围和类实例交互

javascript - jPlayer 不会在 iOS 上播放第二次(有时)

javascript - async/await 在 jQueryeach() 中未按预期工作

javascript - 计算 2 个数组中的每个值,如果每个数组的长度不相同,则推送长度较长的值

javascript - 如何使用 AngularJS 合并两个对象?

javascript - 嵌套 ng-repeat 中的单选按钮