0

I'm trying to make one of my first AngularJS apps, and I have problems trying to use multiple controllers. What I have is :

.when('/tickets',
        {
            controller: 'TicketsController',
            templateUrl: 'views/Tickets.html'
        })

And I want to do something like this

.when('/tickets',
        {
            //i know this dont work
            controller: ['TicketsController', 'OtherEntityController'],
            templateUrl: 'views/Tickets.html'
        })

I've tried ng-controller but once it's there controllers can't know stuff about each other.

I thought using a directive but is too much just for a few options:

I could put my OtherEntityController inside TicketsController but thats not a good way to organize my code.

arman1991
  • 1,166
  • 1
  • 18
  • 28
cristiandley
  • 559
  • 1
  • 5
  • 19
  • 2
    Perhaps a service would suit your needs? Hard to say with scarce information, but take a look at the service docs, here: https://docs.angularjs.org/guide/services – Fissio Jun 09 '15 at 13:19
  • It's not clear what you're trying to do. Can you be more specific? – pablochan Jun 09 '15 at 13:20
  • If you trying to share couple of values between controllers you can use rootScope or services and factories – Tomas Jun 09 '15 at 13:21
  • You cannot have multiple controllers, since controllers are unique entry points to your application. You can have embedded controllers though (for example OtherEntityController inside TicketsController : https://docs.angularjs.org/guide/controller#scope-inheritance-example – arthur.flachs Jun 09 '15 at 13:23
  • 2
    @Tom Please don't advice to use rootScope, it's a bad practice. Services and factories are the way to go :) ! As the other said we need more information about Why you're trying to bind two controllers. if it's you first project i'd recommend to take a look at ui-router instead of the default angular router system. http://www.funnyant.com/angularjs-ui-router/ – Okazari Jun 09 '15 at 13:24
  • @Okazari If rootScope would be bad practice, they wouldn't include it in angular... rootScope is there for a reason, it migh not be good for what is he trying to do, but you cannot simply say that using rootScope is bad practice – Tomas Jun 09 '15 at 13:25
  • 3
    Create a Service and Inject it as a dependency to controller and call service to get the value you want like Service.getValue() – Kumar Garapati Jun 09 '15 at 13:28
  • @Tom I would love to argue about that (because it's an interesting suggest) but i guess it's not that place. Maybe do you want to have little chat about that ? scope.$apply and $broadcast are also included in angular, and it's still a bad practice to use it. it's more like internal angular functions. You can only use it if you really know why you're using it. If you don't know, that mean there is always a clean solution instead. – Okazari Jun 09 '15 at 13:30
  • 1
    @Okazari I have never heard of that $broadcast would be a bad practice... $apply is different story of course. I would say it really depends on what is he trying to do and how big the application is. Sometimes there is just no point flooding application with services when you can store that one value you need in rootScope – Tomas Jun 09 '15 at 13:33
  • @Tom then i'll moderate my words. $rootScope and $broadcast almost always leads to bad uses. Anyone using them should really know what he is doing. That's why i wouldn't advice it to a beginner. – Okazari Jun 09 '15 at 13:40
  • @Cley - can you just explain what is the reason to use 2 controllers for only 1 template? – arman1991 Jun 09 '15 at 13:44
  • @arman1991 i have ticket <-> responses and i would love to be able to delete responses, and edit those too. I can actually put my controller inside ticket controller... but i want to organize plus im testing and learning Angular.js. Directive for that seems too much... i made a Service and a Controller for responses. I want to use my controller. It is good if i Inject my `OtherEntityController` inside `TicketsController` ? – cristiandley Jun 09 '15 at 15:44
  • The default angular router cannot handle 2 controllers under the same url and template. As @Okazari says, the option is angular-ui-router, for more info, read [this](https://medium.com/opinionated-angularjs/advanced-routing-and-resolves-a2fcbf874a1c). My advice also is to create a specific service or factory to handle your problem. To see how to share values between two controllers, here is a [good topic](http://stackoverflow.com/questions/12008908/how-can-i-pass-variables-between-controllers) Hope this will push you forward. – arman1991 Jun 09 '15 at 17:20

0 Answers0