javascript - Subscription not working angular2 -
i have angular2 program in want update data of component when click made on component. have made service in have 2 methods 1 updating data , other getting data.
my service follows
import { injectable } '@angular/core'; import { subject } 'rxjs/subject'; import { observable } 'rxjs/observable'; import { behaviorsubject } 'rxjs/behaviorsubject'; @injectable() export class dataserviceservice { public subject = new subject<any>(); sendmessage(message: any) { debugger; this.subject.next({ text: message }); } clearmessage() { this.subject.next(); } getmessage(): observable<any> { return this.subject; } }
in component a
click made. have function update data.
onclick() { this._dataserviceservice.sendmessage(this.getjsondata()); }
and in component b
have subscribed way.
ngoninit() { debugger; this.subscription = this._dataserviceservice.getmessage().subscribe(message => { this.message = message; console.log(this.message) debugger; }); }
if subscription in same component works fine in other component not updating data. appreciated.
edit of understanding main issue subscriber component loading after service initialized.
try replacing getmessage() function :
getmessage(): observable<any> { return this.subject.asobservable(); }
it seems of right now, getmessage() not return observable object, explain why subscribe method in ineffective.
Comments
Post a Comment