How to set a variable in another angular 2 component -
i'm building angular 4 application , have 2 separate pages, detail page , edit page, each own component.
when user edits model on edit page, redirect them details page this:
// within retailereditcomponent save(): void { this.retailerservice.updateretailer(this.retailer) .then(() => this.router.navigate(['/admin/retailer'])); }
in admin/retailer page, have retailerdetailcomponent looks this:
export class retailerdetailcomponent { retailer: retailer; public hasbeenupdated: boolean = false; constructor( private retailerservice: retailerservice, ) { console.log("in retailerdetailcomponent"); } }
how can set bool hasbeenupdated true other page?
update:
i idea of eventemitter, running problems.
my retailerservice:
export class retailerservice { public onretailerupdated = new eventemitter();; updateretailer(retailer: retailer): promise<retailer> { this.onretailerupdated.next(true); return new promise((resolve, reject) => { apigclient.retailervput({}, retailer, {}) .then(function (result) { resolve(result.data[0]) }).catch(function (result) { reject(result) }); } }) }); } }
and in retailerdetailcomponent:
export class retailerdetailcomponent implements loggedincallback { retailer: retailer;
public showerror: boolean; constructor( private retailerservice: retailerservice) { } ngoninit(): void { this.retailerservice .onretailerupdated .subscribe(value => { this.showerror = true; console.log('event thingy worked') }); }
but console message doesn't show up
the best way use subscription eventemitter. example below:
- you can define event emitter in retailer service:
public onretailerupdated = new eventemitter< boolean >();
- then, in "updateretailer" method, add line:
this.onretailerupdated.next(true);
- subcribe event in retailerdetailcomponent:
this.retailerservice .onretailerupdated .subscribe(value => { // perform appropriate action here });
private subscription; ngoninit() { this.subscription = this.retailerservice.onretailerupdated .subscribe(value => { // perform appropriate action here }); }; ngondestroy() { this.subscription.unsubscribe(); };
Comments
Post a Comment