2

correct me If I am wrong..

--Setting a module
    var app = angular.module("myapp", []);

// here brackets means module does't exist just go and create it, and the empty array is the list of module it depends on

--Getting a module
    var app = angular.module("myapp");

//and if brackets are not here mean module exist just find it.

but what is the difference between below lines:-

  var myapp = angular.module("myapp", []);
  myapp.controller("MyController", "$scope", [function($scope){
    //TODO
  }]);

  and 

  angular.module("myapp", [])
  .controller("MyController", "$scope", [function($scope){
    //TODO
  }]);

I think we should use variable only if our module is divided in multiple files. but I am not sure if it will really makes any difference.

As per Angular Style Guide we should not use variable while declaring a module.Could anyone please clarify?

Thanks

3
  • 1
    Did you mean to use myapp in your first example? Also, your second example would not work, you would need to chain the controller definition with the module definition in the previous line (hint: no semicolon, no app variable). Leaving these two issues aside, both examples are equivalent and produce the same result. Commented Sep 29, 2015 at 12:06
  • I have updated my question Commented Sep 29, 2015 at 12:09
  • You still need to correct the variable name in your first example. ;) The only difference between the examples is that one creates a new variable in the global scope, which you should usually avoid, but you can also prevent this from happening by wrapping your declaration into an IIFE. Commented Sep 29, 2015 at 12:14

1 Answer 1

2

--Setting a module

var app = angular.module("myapp", []);`

Here brackets means module does't exist just go and create it, and the empty array is the list of module it depends on

--Getting a module

var app = angular.module("myapp");`

// and if brackets are not here mean module exist just find it.

Correct

we should not use variable while declaring a module.Could anyone please clarify

When you write: var myapp = angular.module("myapp", []); you are creating the variable myapp in the global scope.

I think we should use variable only if our module is divided in multiple files

That's not necessary. You can always get your module with angular.module("moduleName");, avoiding thus the creation of a global variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.