Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 752
Brian Park Avatar asked Mar 02 '14 22:03

Brian Park


Video Answer


1 Answers

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

like image 169
marcinn Avatar answered Sep 20 '22 12:09

marcinn