描述:

  复制一个对象或者一个数组(好吧,万物皆对象,数组也是一个对象)。
  如果省略了destination,一个新的对象或数组将会被创建出来;
  如果提供了destination,则source对象中的所有元素和属性都会被复制到destination中;
  如果source不是对象或数组(例如是null或undefined), 则返回source;
  如果source和destination类型不一致,则会抛出异常。
  注意:这个是单纯复制覆盖,不是类似继承

使用方法:

  angular.copy(source, [destination]);

参数:

参数名称 参数类型 描述
source * 被copy的对象. 可以使任意类型, 包括null和undefined.
destination (optional) Object||array copy去的目的地. 可以省略, 如果不省略, 其必须和source是同类

返回值:

  返回复制或更新后的对象。

实例:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <script src="js/angular.min.js"></script>
        <script type="text/javascript">
            angular.module('copyExample', []).controller('ExampleController', ['$scope', function ($scope) {
            $scope.master = {};
            $scope.update = function (user) {
                //将user复制后,赋值给master;
                    $scope.master = angular.copy(user);
            };
            
            $scope.reset = function () {
                // 将复制后的$scope.master,赋值给$scope.user,因为$scope.master = {},所以点击RESET,会清空邮箱内容
                angular.copy($scope.master, $scope.user);
            };
            $scope.reset();
            }]);
        </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" />(输入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>
</body>
</html>

点击sava按钮之前效果:

点击sava按钮之后的效果:

点击reset之后的效果:

AngularJS方法 —— angular.copy的更多相关文章

  1. AngularJS方法 —— angular.bootstrap

    描述: 此方法用于手动加载angularjs模板 (官方翻译:注意基于端到端的测试不能使用此功能来引导手动加载,他们必须使用ngapp. angularjs会检测这个模板是否被浏览器加载或者加载多次并 ...

  2. AngularJS方法 —— angular.bind

    描述: 上下文,函数以及参数动态绑定,返回值为绑定之后的函数. 其中args是可选的动态参数,self在fn中使用this调用. 使用方法: angular.bind(self,fn,args ); ...

  3. AngularJs angular.bind、angular.bootstrap、angular.copy

    angular.bind 返回一个调用self的函数fn(self代表fn里的this).可以给fn提供参数args(*).这个功能也被称为局部操作,以区别功能. 格式:angular.bind(se ...

  4. 夺命雷公狗—angularjs—25—angular内置的方法(高级)

    查看版本信息 angular.version console.log(angular.version); 判断是否相等 angular.equals() var str1 = ''; var str2 ...

  5. Angular - - angular.bind、angular.bootstrap、angular.copy

    angular.bind 返回一个调用self的函数fn(self代表fn里的this).可以给fn提供参数args(*).这个功能也被称为局部操作,以区别功能. 格式:angular.bind(se ...

  6. angular.copy()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. angular : copy vs extend

    While using AngularJS, we come across some situation in which we need to copy one object to another ...

  8. 升级 AngularJS 至 Angular

    Victor Savkin 大神撰写了一系列文章详细介绍如何升级 AngularJS 应用: NgUpgrade in Depth Upgrade Shell Two Approaches to Up ...

  9. angular JS中 ‘=’与angular.copy的区别

    先来看代码: <b>{{test1}}</b> <input type="text" ng-model="test2" title ...

随机推荐

  1. gulp安装教程

    1.安装nodejs并选装cnpm: npm install cnpm -g --registry=https://registry.npm.taobao.org 2.全局安装gulp: cnpm i ...

  2. Collective Mindsets (medium) (逻辑题)

    B - Collective Mindsets (medium) Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I6 ...

  3. java Web程序使用wro4j合并、压缩js、css等静态资源

    在Web项目中,js.css合并压缩,不仅有利于减少Http请求数量.减少宽带资源占用,还能有效的管理各种js.css的引入,使整个项目更加有序.而对于访问用户来说,其更大的好处是增加了页面的打开速度 ...

  4. URAL 1181 Cutting a Painted Polygon【递归+分治】

    题目: http://acm.timus.ru/problem.aspx?space=1&num=1181 http://acm.hust.edu.cn/vjudge/contest/view ...

  5. HTML/CSS/JS初始化

    CSS <link type="text/css" href="http://www.mazey.cn/css/mazey-base.css" rel=& ...

  6. 使用JSTL在页面前的空行怎么去除?

    解决的方法是:在每个JSP的头上加上一段代码   <%@ page trimDirectiveWhitespaces="true" %>

  7. MVC ViewBag不能使用在工程文件中添加引用

    在工程文件中 <ItemGroup> // ... </ItemGroup> 添加引用 <Reference Include="Microsoft.CSharp ...

  8. ncl 实例参考

    NCL中绘制中国任意省份的精确地图 NCL学习笔记(实战篇) 用NCL画垂直风场剖面图实例 NCL学习笔记(天气分析图)

  9. Yii2学习笔记---内附GridView配置总结

    1./vendor/yiisoft/yii2/web/UrlManager.php 方法createUrl 修改url参数转码2.config/web.php 配置文件Yii::$app(应用主体)的 ...

  10. [原创]java WEB学习笔记34:Session 案例 之 解决表单重复提交

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...