Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have more than one controllers in AngularJS correctly?

I am getting started with AngularJS, and as I understand, I can have different controllers for different sections of my web page. I am having the problem getting it work. I have two sections of my page and corresponding to each one ng-controller - JSFiddle. Only the section that come first works. For example currently, app1 works fine, but when I move it below app2, only app2 works fine. What could be wrong? Much appreciate any explanation regarding why this behavior and any links.

like image 245
ykesh Avatar asked Dec 13 '13 07:12

ykesh


People also ask

Can we have multiple controllers in AngularJS?

An AngularJS application can contain one or more controllers as needed, in real application a good approach is to create a new controller for every significant view within the application.

Can we inject one controller into another controller in AngularJS?

You can't inject controllers into one another.

Can we have multiple modules in AngularJS?

Yes, you can define multiple modules in angularJS as given below. The modularization in AngularJS helps us to keep the code clarity and easy to understand, as we can combine multiple modules to generate the application.


4 Answers

You can have multiple controllers, but you cannot have multiple ng-app directives on the same page. This means you should only have a single ng-app directive in your html that points to a single module that will be used in your application.

You then define this module and define all your controllers in this module:

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

app.controller('TextController', function ($scope) {
    //Controller Code Here    
});

app.controller('ItemController', function ($scope) {
    //Controller Code Here
});

If for some reason you want to have controllers in separate modules, you can still do that, and include those modules as dependencies of your main module:

var items = angular.module('items', []);
var text = angular.module('text', []);
var app = angular.module('app', ['items', 'text']);


text.controller('TextController', function ($scope) {
    //Controller Code Here
});

items.controller('ItemController', function ($scope) {
    //Controller Code Here
});

Generally you don't need to have a module for each controller. Modules are used to group related pieces of functionality together to make it easy to take that and re-use it in another application.

Here are links to some examples:

Single Module : http://jsfiddle.net/36s7q/4/

Multiple Modules: http://jsfiddle.net/36s7q/5/

Notice how in both example there is only a single ng-app on the page.

like image 93
Daniel Tabuenca Avatar answered Oct 08 '22 18:10

Daniel Tabuenca


Take a look at this, I changed a lot around. http://jsfiddle.net/36s7q/6/

No need for two app modules on the page to achieve two controllers, you can have multiple controllers within the same module. I also simplified the syntax. Take a look.

var items = angular
.module('app1', [])
.controller('ItemController', function($scope) {
$scope.items = [ {
    title : 'Pencil',
    quantity : 8,
    price : 4.2
}, {
    title : 'Pen',
    quantity : 2,
    price : 5.2
}, {
    title : 'Watch',
    quantity : 3,
    price : 10.2
} ];
})
.controller('TextController', function($scope) {
$scope.text = {
    message : 'Welcome!!'
};
});
like image 30
rjm226 Avatar answered Oct 08 '22 16:10

rjm226


Here is how you have multiple controllers:

       var app = angular.module('MyApp', ['ngMaterial', 'ngMessages', 'material.svgAssetsCache']);

 app.controller('DemoCtrla', DemoCa)
    .controller('DemoCtrlb', DemoCb)
    .controller('DemoCtrlc', DemoCc);


        function DemoCa($mdConstant) {
                        // function here
                    }

        function DemoCb($mdConstant) {
                        // function here
                    }

        function DemoCc($mdConstant) {
                        // function here
                    }

I hope it helps ;)

like image 2
Fery Kaszoni Avatar answered Oct 08 '22 17:10

Fery Kaszoni


Instead of answering your actual question, i suggest usage of routing.

Be aware: This technique is not needed to solve your issues. However, you may want to know about it for future projects.

If i got you right, all you want to do is using a different controller / view for a specific section of your page.

To achieve this, create a single application module (remember, Angular applications are SPA's). Then, you could define some routes and tell Angular what to use when one of them is demanded:

var app = angular.module('app', ['ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
        when('/', {templateUrl: './partials/views/root.html',   controller: 'rootCtrl'}}).

        when('/section', {templateUrl: './partials/views/section.html',   controller: 'sectionCtrl'}}).

        otherwise({redirectTo: '/'});
}]);

Further reading: http://docs.angularjs.org/api/ngRoute

Be aware that the latest stable version of Angular requires the ngRoute module in order to use the routeProvider.

like image 1
Wottensprels Avatar answered Oct 08 '22 16:10

Wottensprels