Angular.js – using angular.extend() function

A simple Angular.js example of extending one object with the contents of another object
<!DOCTYPE html>
<html ng-app="testApp">
<head>

<style>

</style>

    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="Tizen Angular.js example"/>

    <title>Tizen Angular.js example</title>

    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>

    <script src="js/jquery-2.1.4.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/angular.min.js"></script>
    <script>

        var app = angular.module('testApp', []);
            app.controller('AppController', function($scope) {

          var sampleObj1 = {};
              sampleObj1.weight = 60;
              sampleObj1.height = 180;
              sampleObj1.depth = 60;
              sampleObj1.rotation = 45;

          var sampleObj2 = {};
              sampleObj2.colors = {};
              sampleObj2.colors.r = 100;
              sampleObj2.colors.g = 178;
              sampleObj2.colors.b = 245;

            $scope.obj1 = sampleObj1;
            $scope.obj2 = sampleObj2;

            $scope.extend = function() {

                angular.extend(sampleObj1, sampleObj2);

            }

        });

    </script>
</head>
<body>
  <br><br>
  <div ng-controller="AppController">
    <button ng-mousedown="extend()">
         Click this button to extend the first object with the contents of the second object
    </button>
    <br><br>
    {{ obj1 }}
    <br><br>
    {{ obj2 }}
  </div>
</body>

</html>

Responses

0 Replies