首页 技术 正文
技术 2022年11月18日
0 收藏 380 点赞 4,064 浏览 1848 个字

描述:

  复制一个对象或者一个数组(好吧,万物皆对象,数组也是一个对象)。
  如果省略了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按钮之前效果:

AngularJS方法  —— angular.copy

点击sava按钮之后的效果:

AngularJS方法  —— angular.copy

点击reset之后的效果:

AngularJS方法  —— angular.copy

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,110
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,202
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,837
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,920