Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - How to route to a controller in different module

Tags:

angularjs

I have a question related to modules and routing in angularjs. The requirements are to define all routing configs in one module - lets call this ModuleRtr.

Once the route is triggered, it should call a controller (called TestCtr for example). This controller is defined in another module - let's call it ModuleCtrl.

If I try to do that, I got an exception like:

"Argument TestCtr is not a function, got undefined"

. The reason is that when angular broadcast $routeChangeSuccess event, the listener tries to access the controller (TestCtr) as part of the current module - ModuleRtr. Because it's not there, it throws an error.

If I simply create TestCtr controller in ModuleRtr (the same that has the route in it), then everything works. But I don't want that, I want to have my TestCtr in different module. I also don't want to have route define in ModuleCtrl module.

So, in more straightforward way, my question is: Is it possible a route defined in one module (ModuleRtr) to call a controller defined in another module (ModuleCtrl)?

Something that I forgot to mention...I don't want to bind these 2 modules by any means. That of course includes listing a dependency during module's creation. I have already tried dynamic loading of TestCtr - it didn't solve the problem.

The scenario is: You have 2 angularjs applications (modules). They don't know anything about each other...except one thing - somewhere in a shared/common location, there is an object which follows a particular structure (like a contract). The first module writes data there. Then it runs a common function from the second module (this is actually the question I am asking - how?). Then the second module knows already where to go (it's a contract) what to read and what to do. One way I can do this is to try dynamically generates the string that represents a module dependency when the other module is created. I am not sure if that is a good solution...

Many thanks!

like image 925
ktt Avatar asked Nov 12 '22 19:11

ktt


1 Answers

You can use a single module with submodules for each functionality, such as:

angular.module('myApp.home',[]);
angular.module('myApp.routing',[]);
...modules here...
angular.module('myApp', [
  'myApp.home',
  'myApp.routing'
]);

So you can send a method from myApp.home calling a intermediate method from myApp and this one calling the myApp.routing method.

At least, this is how I see it.

like image 162
mutlei Avatar answered Nov 15 '22 12:11

mutlei