0

I have decent knowledge of angularJS. I came across some codes where the controllers are defined in this way. eg

   (function () {
       angular.module('meanApp')
      .controller('registerCtrl', registerCtrl);
      registerCtrl.$inject = ['$location', 'authentication'];
     }

    })();

I am not able to understand why is the controller designed in this way i.e : inside (function() )()?.

sac Dahal
  • 1,191
  • 2
  • 13
  • 37

1 Answers1

2

It's just a detail about scopes. This type of function is called Self Invoking functions.

Variables and functions created within (function () {}() will be only available inside it, and so will be not global, what is a good practice for many reasons.

Good for read: https://sarfraznawaz.wordpress.com/2012/01/26/javascript-self-invoking-functions/

Edmundo Santos
  • 8,006
  • 3
  • 28
  • 38