yevg yevg - 1 year ago 67
jQuery Question

Using Multiple Angular.js Controllers on same DOM

Im pretty new to Angular.js and trying to implement it on my Node.js app.

Ive had success creating an RESTful API using angular for a single controller but now would like to use two or more controllers on a single DOM.

Specifically, I would like to..

1) Use angular to load a global scope for the DOM template containing things like site title, navigation, and metadata, all of which will be loaded into the head, header, and footer. The scope would be pulled using a GET request to my Node server.

2) Use angular to load the scope for a given page. This would be on a case-by-case basis for each page. The simple case being to load the scope into the template.

I have succeeded in doing both (1) and (2) separately, but combining them throws an error.

Here is my complete template (actually loaded in parts by Node):

Head - containing ng-controller="configCtrl" for global configuration scope

<html ng-app="angular" ng-controller="configCtrl" ><head>

<meta http-equiv="Content-Type" content="text/html charset=UTF-8" />
<meta name="description" content="{{context.description}}">
<meta name="author" content="{{}}">

<!-- Angular -->
<script src=""></script>

</head><body><div id="wrapper" >

Body - containing ng-controller="testCtrl" for this particular page scope

<div class="container" ng-controller="testCtrl">
<div class="gutter col hidden-xs">&nbsp;</div>
<div class="content col" >

<div class="py80">



<div class="gutter col hidden-xs">&nbsp;</div>

<script src="/public/js/angular/test_controller.js"></script>

Footer - includes the link to controller for global scope

<footer id="footer" ></footer>
</div></body></html> <!-- close tags-->

<script src="/public/js/angular/config_controller.js"></script>

Here is my Angular controller for the "configuration scope"

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

app.controller('configCtrl', function($scope, $http) {

console.log("config controller");



$scope.context =;



Here is my Angular controller for the page scope

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

app.controller('testCtrl', function($scope, $http) {

console.log("test controller");



$scope.context =;



I have some server-side controllers returning data back to angular, as mentioned, they each work when used independently but using them together (as shown above) throws the following client-side error:

angular.js:13920 Error: [ng:areq]
at angular.js:38
at sb (angular.js:1892)
at Pa (angular.js:1902)
at angular.js:10330
at ag (angular.js:9451)
at p (angular.js:9232)
at g (angular.js:8620)
at g (angular.js:8623)
at g (angular.js:8623)
at g (angular.js:8623)

Any suggestions?

2ps 2ps
Answer Source

The problem is you are initializing the app twice. Do this only once:

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

And you’ll be fine. Best practice is to separate that line out into another file entirely (like 'app-initialize.js') and then, you can just do:


in all of your files.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download