试图理解angular.copy

时间:2016-08-14 00:24:00

标签: javascript angularjs

我试图了解angular.copy会做什么。我在Angular js doc中看到了一个例子,但是在填写所有文本字段并单击“保存”按钮后无法确定为什么重置按钮不起作用。但是当我们点击重置按钮然后点击保存之前它就有效。有人请解释。谢谢提前

的index.html:

<div ng-controller="ExampleController">
  <form novalidate class="simple-form">
    <label>Name: <input type="text" ng-model="user.name" /></label><br />
    <label>Age:  <input type="number" ng-model="user.age" /></label><br />
    Gender: <label><input type="radio" ng-model="user.gender" value="male" />male</label>
            <label><input type="radio" ng-model="user.gender" value="female" />female</label><br />
    <button ng-click="reset()">RESET</button>
    <button ng-click="update(user)">SAVE</button>
  </form>
  <pre>form = {{user | json}}</pre>
  <pre>master = {{master | json}}</pre>
</div>

script.js:

angular.
  module('copyExample', []).
  controller('ExampleController', ['$scope', function($scope) {
    $scope.master = {};

    $scope.reset = function() {
      // Example with 1 argument
      $scope.user = angular.copy($scope.master);
    };

    $scope.update = function(user) {
      // Example with 2 arguments
      angular.copy(user, $scope.master);
    };

    $scope.reset();
  }]);

1 个答案:

答案 0 :(得分:0)

在点击重置之前,

$scope.user甚至不存在。