lifecycle - What is the best method to update a components data from a data change in another component? Angular 2 -
so basically, have made generic component displays objects data. data sent via button click. generic component reads data during oninit life cycle hook looks this.
ngoninit() { this.settingsservice.getallsettings().responsedata() .subscribe(settingsstream => { this.settingslist = settingsstream.result }, (err) => { // error messages }, () => { // complete messages this.currentsetting = this.settingslist .findindex(setting => setting.configurationname === this.settingsservice.selectedsetting.selectconfig); }) } once has found configuration name , matches selected name. displays correct value in template so.
<input size="150" [value]="settingslist[currentsetting].configurationname" [(ngmodel)]="settingslist[currentsetting].configurationname"> however generic components data can changed via button click. because generic component reads data during oninit not change data displays. have tried using onchanges , docheck lifecycle hook not allow me alter data looking changes.
this method in template activates , displays component button click
<div *ngfor="let settings of settingslist"> <button class="btn btn-secondary" (click)="changesection(settings.configurationname)" (click)="domelement='generic'"> {{settings.configurationname}} </button> </div> <div *ngif="shownewconfig"> <new-config> </new-config>
i had working separate components seems inefficient , duplication of code.
all appreciated many thanks!
i may misunderstood looking for, cannot put in comment:
ngoninit(){ loaddata() } loaddata(){ this.settingsservice.getallsettings().responsedata()..... } <button (click)="loaddata()">load</button>
Comments
Post a Comment