When I create a new controller using VS2015 templates I get this code:
(function () {
'use strict';
angular
.module('app')
.controller('controller', controller);
controller.$inject = ['$scope'];
function controller($scope) {
$scope.title = 'controller';
activate();
function activate() { }
}
})();
Questions:
Why does VS template wraps the code in a self invocing function?
What's the thing with the
activate()function? What code do I suppose to write inside and why do I need a seperate function instead of just writing the code inside the controller?Do the
controller.$inject = ['$scope'];considered a better practice then writing the dependencies in an array (as an argument of the controller function).
Because it prevents corrupting global namespace. In this case,
controllerwill become a member of global namespace in the absence of IIFE.It helps separating declaration and execution of controller variables and code respectively. Prior to calling the
activatefunction, we usually declare all the dependencies as the controller members usingthis.syntax.You will write the executing code inside the
activatefunction.Actually yes! It helps you to separate your controller definition from it's angular counterpart. It helps you to avoid writing the entire controller code in
angular.module( ... ).controllerblock to improve readability of your code.Edit 1:
Without the IIFE, the function named
controllerwill become a member of the global namespace, and will be accessible throughout your page. It will also pose a possiblity of being overwritten by subsequent code. Don't mix AngularJS with this, because this is something which JavaScript does.The
activatefunction is just a layer to separate the concerns. You can write your code entirely in the functioncontroller. But in that way, it becomes harder to separate which code controller is executing, and which code binds the variables to controller. Take this example:By reading the above code, one can easily know about the variables being used down the line in the controller, without specifically going into the depth of the controller. By following this convention, we will always know that the controller will start executing the business logic right where
activate()is called.