欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談angular.copy() 深拷貝

 更新時(shí)間:2017年09月14日 09:30:16   作者:愛吃菠蘿蜜的小透明  
本篇文章主要介紹了淺談angular.copy() 深拷貝,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

因?yàn)轫?xiàng)目中需要拷貝,查閱angularjs API文檔,發(fā)現(xiàn)對(duì)angular.copy() 的解釋:

復(fù)制一個(gè)對(duì)象或者一個(gè)數(shù)組(好吧,萬物皆對(duì)象,數(shù)組也是一個(gè)對(duì)象)。

1> 如果省略了destination,一個(gè)新的對(duì)象或數(shù)組將會(huì)被創(chuàng)建出來;
2> 如果提供了destination,則source對(duì)象中的所有元素和屬性都會(huì)被復(fù)制到destination中;
3> 如果source不是對(duì)象或數(shù)組(例如是null或undefined), 則返回source;
4> 如果source和destination類型不一致,則會(huì)拋出異常。 注意:這個(gè)是單純復(fù)制覆蓋,不是類似繼承。

使用方法:

angular.copy(source, [destination]);

參數(shù):

參數(shù)名稱 參數(shù)類型 描述
source * 被copy的對(duì)象. 可以使任意類型, 包括null和undefined.
destination (optional) Object,array copy去的目的地. 可以省略, 如果不省略, 其必須和source是同類

返回值:

返回復(fù)制或更新后的對(duì)象

<!DOCTYPE html>
<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="copyApp">
    <div ng-controller="CopyController">
      <form novalidate class="simple-form">
        姓名: <input type="text" ng-model="user.name" /><br /> 
        年齡:<input type="number" ng-model="user.age" /><br /> 
        郵箱: <input type="email" ng-model="user.email" /><br />
        性別:<input type="radio" ng-model="user.gender" value="male" /> 男
        <input type="radio" ng-model="user.gender" value="female" /> 女
        <br />
        <button ng-click="reset()">重置</button>
        <button ng-click="update(user)">保存(拷貝)</button>
      </form>
      <pre>form = {{user | json}}</pre>
      <pre>master = {{master | json}}</pre>
    </div>

    <script>
      angular.module('copyApp', [])
        .controller('CopyController', ['$scope', function($scope) {
          $scope.master = {};
          $scope.update = function(user) {
            $scope.master = angular.copy(user);
            console.log($scope.master);
          };
          $scope.reset = function() {
            angular.copy($scope.user, $scope.master);
            console.log($scope.master);// Object { }
            console.log($scope.user); //undefined
          };
          $scope.reset();
        }]);
    </script>
  </body>

</html>

效果圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論