I would make it a mix of the two:
(function (angular){
'use strict';
var mod = angular.module('modName'))
mod.controller('MainCtrl', function() {});
}(angular);
I think it’s clearer to define the module right before you use it (rather
than have to hunt for the definition at the bottom of the file. You want to
wrap everything in a self-executing anonymous function to prevent variables
such as var mod to leak into the global scope. Calling the function with
angular at the buttom isn’t necessary (I often ommit it). The only thing it
does is protect you in the unlikely situation where the global “angular”
variable is or needs to be renamed. In that case you would just need to
rename the bottom.
One question, what is the ‘undefined’ parameter for in your example?
On Tuesday, December 24, 2013 2:24:38 AM UTC-8, Ashwini shripatrao wrote:
Hi,
>
> Please let me know which one is the better way to create controller,
> (function (ng, mod, undefined) {
> 'use strict';
> mod.controller('MainCtrl', MainCtrl); ......
> }(angular, angular.module('modName')));
> Or
> var myAppModule = angular.module('myApp', []);
> myAppModule.controller('MainCtrl',function() {});
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.