AngularJS - Communicating Between Controllers

preview_player
Показать описание
This video tutorial covers communicating and sharing state between controllers using a shared service and $rootScope.

You can grab the code here:

With a BONUS fiddle using a directive

If you have any technical questions check out the awesome mailing list here:
Рекомендации по теме
Комментарии
Автор

I like your accent, it is very clear word by word. Because English is not my 1st lang.
More angularjs tuts, thanks

vzhen
Автор

They are for dependency injection. AngularJS will attempt to automatically figure out your dependencies for you, but if you specify them in the $inject line, it's faster and error-free. Note: You will definitely need the $inject lines if you minify and/or munge your production code, because the variable names will have changed.

mmoussa
Автор

Disagreed! This video provided exactly the info I needed to get through a stumbling block I was running into. Sometimes, just a little info by itself is perfect!

akronymn
Автор

Great video... I was concerned about this in a current project I'm working on... I was also thinking events would be a way to go... Glad to see angular has a fancy way of doing it... A little confused about some of the other negative comments below... I wish people would actually back up there criticisms with better alternatives than what was presented in this video...

ajcroteau
Автор

OMG thank you a million, this was the only way I could get one of my controllers to communicate correctly with my factory. Genius! 

DanielRamBeats
Автор

This video is really awesome ! I it seems this is what I need to enquire the state of given controller from another controller.

However, this does not work using "invoke" function. It works using "trigger" action. So effectively, if a controller carries out an action, and has a new state, then, it will have to broadcast the state, and it is up to other controllers to listen to that broadcast and respond accordingly. Or better, perform the action in the shared service, then broadcast the state.

Please tell me if my understanding is correct.

TarekFaham
Автор

Simpulton Great tutorials keep them coming. Question regarding Basic Auth on a resource, any idea how this can be done?

fowlerjustin
Автор

This is a really nice, succinct example. Thank you.

BanksySan
Автор

I wasn't sure why the inject lines were needed, so i commented them out and it still worked... What are they for?

stonekeeper
Автор

Hi Simpulton, Thanks for the video. Its clearly explained how we can transfer the data between the controllers.
I am new to angular so I have couple of questions here, Its  mandatory to use factory services to handle the rootscope? Can we have parent controller something like a main controller and within the parent controller can we have 1 or more child controllers and  communicate via parent? 

kishorekumaru
Автор

This is essentially event broadcasting using an event bus. How would you personally improve upon this? I have some ideas outside of the scope of this example but I am curious what your thoughts are.

simpulton
Автор

Thanks for explaining in simple words.

BhalchandraKadamITGuy
Автор

Great tutorial, but I have a concern here as in is it advisable to use both shared services and $rootScope in a single application ?
Because from what I have read about AngularJS, either of the approaches should be taken since they are meant to serve the same purpose..
Please correct me if I am wrong ...
Thanks

nishhhaaa
Автор

How so? You need to broadcast the event anyway regardless of whether or not you are using the shared service.

mmoussa
Автор

Thanks for your reply.
So, if I understand you correctly, you imply that any data that needs to be shared has to be in the shared service.
$rootScope should only be used in case of event broadcast as you mentioned in case of broadcast or init !!!

Could you provide me a more comprehensive example or a link to some angular app(sample) highlighting this in much detail, because I have kind of mixed up shared service and RootScope in my application and it has all become a mess !!!!

nishhhaaa
Автор

very interesting. this somewhat resembles the Flux architecture design pattern

inspird
Автор

Hi Simpulton.
I wanted to know, which IDE you were using in the demo?
Thanks in Advance.

bhupendrasyadav
Автор

You could but controllers should be fine grained to the view they control and therefore you are not limited to using $rootScope as an event bus in the controller but rather constrained by what a controller should actually be doing and what it should own.

Does this make sense?

simpulton
Автор

you could "$watch" the shared model which injected  in the creation of every controller.

XinWongDigital
Автор

Good points... sounds like great material for a blog post nudge! :D

simpulton
visit shbcf.ru