angular.copy


angular.copy Function – It basically creates the copy of the source array or object. The syntax for angular copy function is angular.copy(source, [destination]); where source will be copied into destionation.
source and destionation both must be of the same type.

Here in this tutorial we are going to explain the ng function component with example and demo. You can use our online editor to edit and run the example.


angular.copy Syntax | Example | Demo

Syntax

Syntax of angular copy function is as –

angular.copy Syntax :

angular.copy(source, [destination]);

Arguments

  • source(Any Type) – Source to be copied. It can be any type – null, undefined or array.
  • destination(object or array) – Destination to which source to be copied. It must be object or array.

Example

Here is simple example of angular copy function-

angular.copy Example :

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
 $scope.master = {};
 $scope.reset = function() {
	  // copy data from master to user 
	 $scope.user = angular.copy($scope.master);
    };

    $scope.saveUser = function(user) {
      // copy user data in master
      angular.copy(user, $scope.master);
    };
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController"> 
 
 <form novalidate class="simple-form">
    <label>Name: <input type="text" ng-model="user.name" /></label><br />
    <label>Email:  <input type="email" ng-model="user.email" /></label><br />
	<label>Phone:  <input type="text" ng-model="user.phone" /></label><br />
    <button ng-click="reset()">RESET</button>
    <button ng-click="saveUser(user)">SAVE USER</button>
	<pre>user = {{user | json}}
master = {{master | json}}


Try it »


Advertisements

Add Comment

📖 Read More