1

I’ve read two AngularJS sample on Github. I’m confused to how to modularize controller. The first on is set variable(foodMeApp) and re-use it. Controllers need less arguments. It’s easier to read. But, second one doesn’t use variable(foodMeApp) and has more arguments. I think this way is frequently used on AngularJS samples.
Is this any merit to use the second way?

1.https://github.com/IgorMinar/foodme

var foodMeApp = angular.module('foodMeApp', ['ngResource']);

foodMeApp.constant('CONFIG_A', {
  baseUrl: '/databases/',
});

foodMeApp.controller('HogeController', function HogeController($scope, CONFIG_A) {
    console.log(“less arguments");
});

2.https://github.com/angular-app/angular-app

angular.module('foodMeApp', ['ngResource']);

angular.module('foodMeApp').constant('CONFIG_B', {
  baseUrl: '/databases/',
});

angular.module('foodMeApp').controller('HogeController', ['$scope', 'CONFIG_B', function($scope, CONFIG_B) {
    console.log("more arguments");
}]);

3 Answers 3

1

since angular.module('...') ,constant,provide,controller,factory ... return the same mdoule you can chain module method calls if you want to or not... it's just javascript.

you can write

angular.module('foo',[])
.controller('bar',function(){})
.service('baz',functinon(){})
.constant('buzz',something)
.value('bizz',somethingelse);

it makes no different.

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

Comments

0

This example is using an array for your dependencies which are going to be injected into your controller. The array method is typically used when the code is going to minified at some time and allows angularjs to know exactly which items will be injected. If this array were not there, angular would only see 'a', 'b' as the items injected into the function and would not be able to figure out what those things were. Items in the array are strings and will not be changed when the minification happens.

 angular.module('foodMeApp').controller('HogeController', ['$scope', 'CONFIG_B', 
   function($scope, CONFIG_B) {
        console.log("more arguments");
   }]);

Hope this helps explain the differences which you have seen.

Comments

0

There is no such 'merit' of going for 2nd option. 1st approach is more popular as it avoids repeating angular.module('foodMeApp') so many times. If you give it a name "foodMeApp" you can directly use foodMeApp as you would have used in 2nd approach. Less repetition, simpler life.

1 Comment

By using the first form you are introducing a global JavaScript variable. So while "simpler" it is not necessarily "right"...

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.