AngularJS API之copy深拷貝詳解及實(shí)例
更新時(shí)間:2016年09月14日 14:56:31 作者:xingoo
這篇文章主要介紹了AngularJS API之copy深拷貝詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
angular提供了一個(gè)可以復(fù)制對象的api——copy(source,destination),它會對source對象執(zhí)行深拷貝。
使用時(shí)需要注意下面幾點(diǎn):
- 如果只有一個(gè)參數(shù)(沒有指定拷貝的對象),則返回一個(gè)拷貝對象
- 如果指定了destination,則會深拷貝對象復(fù)制給destination
- 如果source是null或者undefined,那么會直接返回source
- 如果source就是desitination,那么會報(bào)錯(cuò)。
下面看看使用樣例:
<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深拷貝的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對本站的支持!
相關(guān)文章
使用Angular內(nèi)置模塊進(jìn)行HTTP請求
這篇文章主要介紹了使用Angular內(nèi)置模塊進(jìn)行HTTP請求方法步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

