Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have multiple controllers in multiple files. on one page I use two controllers.

Now I get the error '&p1=not%20a%20function%2C%20got%20undefined'

app.js:

var app = angular.module('demo', ['MainControllers', 'MainServices'])
    .constant('myConfig', {
        'backend': 'http://localhost:7645',
});

controller 1:

angular.module('MainControllers', ['ui.bootstrap'])
.controller('DemoOneController', function($scope, $rootScope, $modal, mainService) {

Controller 2:

angular.module('MainControllers', ['ui.bootstrap'])
.controller('DemoTwoController', function($scope, $rootScope, $modal, mainService) {

controller 3:

angular.module('MainControllers', ['ui.bootstrap'])
.controller('ModalDemoCtrl', function ($scope, $modal, $log) {

what is wrong?

share|improve this question
    
you don't need to add the dependencies multiple times, just once: angular.module('MainControllers').controller("...") –  Evandro Silva Dec 11 '14 at 11:43
    
or you could do this: app.controller(""); –  user3856699 Dec 11 '14 at 12:05

1 Answer 1

up vote 0 down vote accepted

when u use angular.module('MainControllers', ['ui.bootstrap']) will override the previous module you define with MainControllers name. you need only one definition of the module so u can do like this,

angular.module('MainControllers', ['ui.bootstrap'])
  .controller('DemoOneController', function($scope, $rootScope, $modal, mainService) {

angular.module('MainControllers')
   .controller('DemoTwoController', function($scope, $rootScope, $modal, mainService) {

angular.module('MainControllers')
    .controller('ModalDemoCtrl', function ($scope, $modal, $log) {

removing , ['ui.bootstrap'] from second and third controllers will solve the overriding issue,

when angular finds module dependency array with or without empty like

angular.module('MainControllers', []) //without dependency

angular.module('MainControllers', ['ui.bootstrap']) //with dependency

it will override the module.

if you do like angular.module('MainControllers').controller('Mo....` //without dependency array

it will check for the module with the name MainControllers which is already defined and assign the controller to that module.

here is the demo Plunker

share|improve this answer
    
now 'DemoTwoController' gives error: "0x800a139e - JavaScript runtime error: [$injector:nomod] http://errors.angularjs.org/1.2.19/$injector/nomod?p0=MainControllers" –  user2257651 Dec 11 '14 at 16:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.