English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

AngularJs API의 copy 깊은 복사 상세 설명 및 예제 코드

angular은 object를 복사할 수 있는 API를 제공합니다. copy(source, destination)는 source object에 대해 깊은 복사를 수행합니다.

사용 시 주의해야 할 사항은 다음과 같습니다:

  1. 파라미터가 하나만 있으면 (복사할 object를 지정하지 않았다면) 복사된 object를 반환합니다.
  2. destination을 지정하면 object를 깊이 복사하여 destination에 복사합니다.
  3. source가 null이나 undefined라면 source를 직접 반환합니다.
  4. source가 destination과 같다면 오류가 발생합니다.

다음 예제를 보겠습니다:

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
</head>
<body ng-app="copyExample">
  <div ng-controller="ExampleController">
    <form novalidate class="simple-form">
      Name: <input type="text" ng-model="user.name" /><br />
      E-mail: <input type="email" ng-model="user.email" /><br />
      Gender: 
      <input type="radio" ng-model="user.gender" value="male" />
      male
      <input type="radio" ng-model="user.gender" value="female" />
      female
      <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>
  angular.module('copyExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.master= {};
    var test1;
    console.log(angular.copy(test1));//undefined
    var test3=null;
    console.log(angular.copy(test2));//undefined
    var test2 = "a";
    // console.log(angular.copy(test2,test2));//error!!
    $scope.update = function(user) {
      // Example with 1 argument
      $scope.master= angular.copy(user);
    });
    $scope.reset = function() {
      // Example with 2 arguments
      angular.copy($scope.master, $scope.user);
      console.log($scope.master);
      console.log($scope.user);
    });
    $scope.reset();
  });
  </script>
</body>
</html>

이것이 AngularJS API의 copy 깊은 복사에 대한 자료 정리입니다. 이후 추가 자료를 계속 추가하겠습니다. 여러분의 이 사이트에 대한 지원에 감사합니다!

언급: 이 문서의 내용은 인터넷에서 가져왔으며, 저작권자는 모두입니다. 내용은 인터넷 사용자가 자발적으로 기여하고 자체적으로 업로드한 것이며, 이 사이트는 소유권을 가지지 않으며, 인공 편집 처리를 하지 않았으며, 관련 법적 책임도 부담하지 않습니다. 저작권 문제가 있는 내용을 발견하면, 이메일을 notice#w로 보내 주시기 바랍니다.3codebox.com(이메일을 보내면, #을 @으로 변경하십시오. 신고하고 관련 증거를 제공하시면, 실제로 확인되면 이 사이트는 즉시 위법 내용을 삭제합니다。)