Brian Park
Brian Park

Reputation: 2577

Handling multiple controllers with the same name when using angular-ui-router

I'm building an application using Angular-UI-Router. I have a top-level Angular module that depends on sub-modules and those sub-modules have controller with the same name as follows:

var moduleA = angular.module('moduleA', []);
moduleA.controller('SameNameCtrl', function () {
  // ModuleA SameNameCtrl implementation
});

var moduleB = angular.module('moduleB', []);
moduleB.controller('SameNameCtrl', function () {
  // ModuleB SameNameCtrl implementation
});

var app = angular.module('app', ['ui.route', 'moduleA', 'moduleB']);

How do I specify controller in different modules when constructing state with Angular-UI-Router?

$stateProvider
  .state('app.stateA', {
    url: '/stateA',
    templateUrl: 'template-A.html',
    controller: ????  // how to specify moduleA SameNameCtrl
  })
  .state('app.stateB', {
    url: '/stateB',
    templateUrl: 'template-B.html',
    controller: ????  // how to specify moduleB SameNameCtrl
  })

Sure, I can assign different controller name for controllers in different module, but I'd like to know if it's possible with controllers having the same name.

Thanks!

Upvotes: 12

Views: 2982

Answers (1)

marcinn
marcinn

Reputation: 1786

Unfortunatelly modules are not namespace mechanism in angularjs. You should come up / follow a naming convention to differentiate the controllers

Upvotes: 3

Related Questions