登录  /  注册
首页 > web前端 > js教程 > 正文

AngularJS API之copy深拷贝详解及实例

高洛峰
发布: 2017-01-03 15:54:16
原创
1338人浏览过

angular提供了一个可以复制对象的api——copy(source,destination),它会对source对象执行深拷贝。

使用时需要注意下面几点:

如果只有一个参数(没有指定拷贝的对象),则返回一个拷贝对象

如果指定了destination,则会深拷贝对象复制给destination

如果source是null或者undefined,那么会直接返回source

如果source就是desitination,那么会报错。

下面看看使用样例:

<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 class="brush:php;toolbar:false">form = {{user | json}}
登录后复制
master = {{master | json}}
登录后复制
<script> angular.module(&#39;copyExample&#39;, []) .controller(&#39;ExampleController&#39;, [&#39;$scope&#39;, 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>

AngularJS API之copy深拷贝详解及实例

以上就是对AngularJS API之copy深拷贝的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

更多AngularJS API之copy深拷贝详解及实例相关文章请关注PHP中文网!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号