angular.merge


angular.merge: This function is used to merge two objects in AngularJs. You can also use this function to merge multiple objects. Here in this tutorial we are going to explain how you can use this function to merge objects in AngularJs. You can use our online editor to edit and run the code online.


angular.merge – AngularJs Merge Objects | Example

Sytax for angular.merge is as below-

Syntax –

angular.merge Syntax

angular.merge(src, dst);

The above function copies the numerable properties from the src(source) object(s) to dst(destination).

Arguments

  • src : Source Object.
  • dst : Destination Object.

Returns

  • Object : Reference to dst.

Now let us create a simple example to understand the above function.

Example

AngularJs Merge Objects 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) {
   var a = {name:'John'};
   var b = {age:'21'};
  $scope.result1 = '';
  $scope.result2 = '';
  $scope.mergeObjects = function() {
    angular.merge(a,b); // will merge b into a
	$scope.result1 = a;
	$scope.result2 = b;
  }
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController">  
    <button ng-click ="mergeObjects()" >mergeObjects </button>
	<br>
	Result for a = {{result1}}<br>
	Result for b = {{result2}}
</div> 
</div>
</body>  
</html>                                                                                                                                                                           

The above example will merge the object b into object a.

If you run the above example it will produce the output something like this –


Advertisements

Add Comment

📖 Read More