angularjs - Javascript Self Invoking functions in Angular controllers -
i use self invoking functions include javascript code inside angular controller (i think saw somewhere best practice). controller looks this:
(function() { // code })()
i use gulp merge controllers 1 file. question this. mean of javascript code invoked , executed when application starts? if so, guess not approach. there solutions issue? comments? thanks
no, not code gets executed. when define angular modules
(function() { 'use strict'; angular .module('mymodule', []) .controller('mycontroller', ['$http', mycontrollerfunc]); function mycontrollerfunc ($http) { // ... } })();
what gets executed angular
methods register module , controller. actual controller logic in callback function, , gets called when controller invoked (e.g. router).
so, good, , wrapping code in anonymous functions idea keep, example mycontrollerfunc
, out of global namespace.
Comments
Post a Comment