Send event from parent to childs without template

42 views
Skip to first unread message

Cedric Cazin

unread,
May 24, 2016, 5:27:35 PM5/24/16
to AngularJS
Hi

I got a parent component which has en @input isEnable, on the setter I want to inform all childs their @input isAnable to the same value.
I don't want to use the template <child [isEnable]='isEnable' > 
but I would like to do like angular js a kind of 'brodcast' to all childs

is this possible ?

regards

Sander Elias

unread,
May 25, 2016, 12:25:51 AM5/25/16
to AngularJS
Hi Cedric,

There are multiple solutions to your issue. The cleanest one is creating a small service that hosts one(or more) observable(s). 
There is actually a good sample about this in the docs. scroll down to "Parent and children communicate via a service". 
While you are on that page, there are some more examples on exactly this subject.

Regards
Sander

Cedric Cazin

unread,
May 25, 2016, 3:37:04 AM5/25/16
to AngularJS
Thanks 
that's what I was 'afraid' of... create a service only for one element

Sander Elias

unread,
May 25, 2016, 3:49:23 AM5/25/16
to AngularJS
Hi Cedric,

You can inject this service only in the component you need it for. And it's not for 1 element, it's for a couple off components/directives right?

Regards
Sander

Cedric Cazin

unread,
May 25, 2016, 4:22:53 AM5/25/16
to AngularJS

parent component < service > child components

only for onIsEnableChanged

Sander Elias

unread,
May 25, 2016, 4:39:51 AM5/25/16
to AngularJS
Hi Cedric,

If it's just 1 single variable, there are other solutions in the page I posted.  Perhaps this pattern might suit you a bit better in this case.

Regards
Sander

Cedric Cazin

unread,
May 31, 2016, 8:36:02 AM5/31/16
to AngularJS
Hi 
thanks for the help, since then I am trying to refactor my project :)

I was wondering what is the best pattern for a configuration class ?
I want the class to send provide event when one of its prop change value.

regards

Sander Elias

unread,
May 31, 2016, 9:45:55 AM5/31/16
to AngularJS
Hi Cedric,

Use an subject. that is an observable that you can use to 'watch' for changes(subscribe), but also to feed changes (put in the 'next' value).

Regards
Sander
Reply all
Reply to author
Forward
0 new messages