0

Is it possible to have more than one ng-controller for one templateUrl when using .when? I need three different ng-controllers and two factories for the same templateUrl. I know I can have many controllers, but I don't know how to with .when, and I need to use .when because I'm using nodejs and expressjs as well.

raflarr
  • 7
  • 6
  • Possible duplicate of [Can I pass multiple controllers in $routeProvider.when() in angularJS?](https://stackoverflow.com/questions/25061540/can-i-pass-multiple-controllers-in-routeprovider-when-in-angularjs) – anoop Oct 30 '17 at 05:53

1 Answers1

0

You can nest those 2 controllers inside one defined in .when in HTML template. So, your HTML template may look like-

<div ng-controller="ctrl2">
    <div ng-controller="ctrl3">
    ...
    </div>
</div>
  • so ctrl2 would go with the $routeProvider .when? and after that ctrl3 would work as well? I have two controllers and I need to use a factory to share some data between those controllers, that's why I don't think that nested solution would work. each controller controls a different section of the html. – raflarr Oct 30 '17 at 06:24
  • Ok. So, you can simply have those two controllers initiated in HTML template without nesting.
    ...
    ...
    like this may be.
    – Harshali Talele Oct 30 '17 at 06:30
  • ok and then what do I do in the $routeProvider; what should I pass to .when? – raflarr Oct 30 '17 at 06:33
  • http://plnkr.co/edit/rQTgwrTUQIUTokcbbtv5?p=preview This might help. In $routeProvider, you can either not pass any controller or have a controller which will be parent of both. – Harshali Talele Oct 30 '17 at 06:38