In angular 2 I have 2 parent components that need to emit and listen but my codes not working -


in angular 2 have 2 parent components need emit , listen codes not working

i have parent component needs broadcast parent component that component can update itself.

this component emitting:

  @output() userupdated = new eventemitter();    userupdated = "userupdated($event)";    userupdated.emit("test"); 

this other parent component listening emit:

  userupdated(item) {     this.getallunreadmessages();   } 

outcome lot of console errors.

i tried follow this: http://learnangular2.com/outputs/

but problem want me pay emit component cannot scenario.

i need basic emit or broadcast component. becoming difficult simple.

i've ever go working parent child components.

but need work parent parent.

ok here's plunker: http://plnkr.co/edit/qmnxg7oxf3sdtptkhuen?p=preview

there lots of answers question on stackoverflow , posting question not first time..

you first make shared service app

import {subject} 'rxjs/subject';  @injectable() export class myservice {      public invokeevent:subject<any> = new subject();      constructor() {} } 

then provide in module components share same instance of component (singleton)

@ngmodule({   imports: [ browsermodule ],   providers: [myservice]   declarations: [ app, anotherapp ],   bootstrap: [ app ] }) export class appmodule {} 

from point on it's usage of subject in components.

if want emit event, call next

this._myservice.invokeevent.next(this.counter++); 

and receive/listen, subscribe:

this._myservice.invokeevent.subscribe((value) => {            console.log(value);             this.name = value;          }); 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -