1

I am new to angularJs. I am confused which one is best way to create a controller for ng-app="mainApp". In programming other programming languages that I had worked on suggest to keep relative data together. But in angularJs it's considered best practice to have new module for controllers when we can define controller over main app module. If we create controller on mainApp it will keep controller and bind which is what we want in other languages.

var myapp = angular.module("mainApp", []);
myapp.controller("testController", function($scope)
{
    $scope.value = "test";
})

//OR

angular.module("mainApp", ["moduleController"]);
angular.module("moduleController", []).controller("testController", function($scope){
    $scope.value = "test";
})

For production environment which one should be used.??

1
  • Have you got your answer? Commented Aug 28, 2015 at 11:37

2 Answers 2

2

Option 1:

var myapp = angular.module("mainApp",[]);
myapp.controller("testController",function($scope)
{
    $scope.value="test";
})

Option 2:

angular.module("mainApp", ["moduleController"]);
angular.module("moduleController",[]).controller("testController",function($scope){
    $scope.value="test";
})

Option 2 is better because this will allow you to write your controllers in separate files. As a result your code will be more readable. It'll also help you if you want to reuse your controllers in other AngularJS projects.

For example, you can write the following code in one file e.g app.js:

angular.module('mainApp',['ngRoute', 'appController']);

And you can write the controller in another file e.g controllers.js:

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

appController.controller('testController', ['$scope',
    function($scope) {
        $scope.value="test";
    }
]);

Now, you can reuse the controllers in another project by just adding the controllers.js file in the project and adding dependency to appController in the app.js file.

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

1 Comment

Even with option 1 you can use different files for your different controller. Look a this example
1

Neither, none of them will run in production environment where all script will be minified. Angular's injector subsystem is able to find and resolve $scope, $location, $etc and provide them to the component as requested.

myapp.controller("testController",function($scope)
{
   $scope.value="test";
})

However, upon minification, the code above will end up looking something like:

a.controller("testController",function(b)
{
   b.value="test";
})

which would cause the dependency injection to fail and result in a runtime error.

You will have to use it as below:

var myapp=angular.module("mainApp",[]);
myapp.controller("testController", ['$scope',function($scope)
{
   $scope.value="test";
}]);

In this case, the controller function is initialized inside of an array after a list of each dependency as string literals. This ensure dependency injection is properly maintained, even through minification or uglification.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.