12

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!

||||||
  • Have you found your answer? I am also looking for the same. – Rohit Jangid May 4 '16 at 13:50
3

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

||||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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