AngularJS has this built-in method for doing object to object copies called angular.extend(). It is a very powerful function that has many uses.
The documentation can be found here:
https://docs.angularjs.org/api/ng/function/angular.extend
Letโs consider a mythical ThingController that looks like this using traditional AngularJS code:
app.controller(โThingControllerโ, [ โ$scopeโ, function($scope) { $scope.thingOne = โoneโ; $scope.thingTwo = โtwoโ; $scope.getThings = function() { return $scope.thingOne + โ โ + $scope.thingTwo; }; }]);
Lots of assignment to $scope to create member variables (models) and methods. This is how it would look if we used angular.extend():
app.controller(โThingControllerโ, [ โ$scopeโ, function($scope) { angular.extend($scope, { thingOne: โoneโ, thingTwo: โtwoโ, getThings: function() { return $scope.thingOne + โ โ + $scope.thingTwo; } }); }]);
Using angular.extend() seems like a cleaner way to express all these assignments to $scope. The only nit I have with this is that models and methods are mixed in some arbitrary order. We could clean it up with code looking something like this:
app.controller(โThingControllerโ, [ โ$scopeโ, function($scope) { // models angular.extend($scope, { thingOne: โoneโ, thingTwo: โtwoโ }); // methods angular.extend($scope, { // in HTML template, something like {{ getThings() }} getThings: function() { return $scope.thingOne + โ โ + $scope.thingTwo; } }); }]);
What if we donโt want application code to be able to store directly to thingOne and thingTwo unless the values are valid? We can implement getters and setters for these and use private variables to hold the valid values:
app.controller(โThingControllerโ, [ โ$scopeโ, function($scope) { // private var _thingOne = โoneโ, _thingTwo = โtwoโ; // models angular.extend($scope, { get thingOne() { return _thingOne; }, set thingOne(value) { if (value !== โoneโ && value !== โtwoโ) { throw new Error(โInvalid value (โ+value+โ) for thingOneโ); }, get thingTwo() { return _thingTwo; }, set thingTwo(value) { if (value !== โtwoโ && value !== โthreeโ) { throw new Error(โInvalid value (โ+value+โ) for thingTwoโ); } }); // methods angular.extend($scope, { // in HTML template, something like {{ things }} get things() { return _thingOne + โ โ + _thingTwo; } }); }]);
We can also use angular.extend() to implement mixins. Consider this contrived logging class:
var debug = true, Logger = { print: function(s) { return debug ? s : โโ } };
We can โmixinโ this Logger class to our $scope in multiple controllers using angular.extend().
app.controller(โControllerOneโ, [ โ$scopeโ, function($scope) { // mixin $scope angular.extend($scope, Logger); // define our $scope angular.extend($scope, { myVar: 1, log: function() { this.print(this.myVar); } }); }]); app.controller(โControllerTwoโ, [ โ$scopeโ, function($scope) { // mixin $scope angular.extend($scope, Logger); // define our $scope angular.extend($scope, { myVar: 2, log: function() { this.print(this.myVar); } }); }]);
In a view controlled by ControllerOne, {{ log() }} will render โ1โ into the DOM (the value of myVar). In a view controlled by ControllerTwo, {{ log() }} will render โ2โ into the DOM. We only had to define the mixin once and were able to use it twice. We could use it as many times as we want, obviously. If we change the debug variable to false, none of the values will be inserted into the DOM at all.
NEW RESEARCH: LEARN HOW DECISION-MAKERS ARE PRIORITIZING DIGITAL INITIATIVES IN 2024.
One caveat to all this. As I write this, the implementation of angular.extend(), jquery.extend(), and jqlite.extend() are all broken. They do not copy getter and setter methods.
Mike Schwartz
Related Posts
-
Rapid Prototyping with AngularJS
Building great web applications is challenging and all great web applications start with a proof…
-
Three issues with AngularJS
AngularJS is all the rage these days, and for good reason. Itโs a lightweight and…