javascript - Angular4, update component on route change -
how update component when route changes. have component :
import { component, oninit } '@angular/core'; import { activatedroute } '@angular/router'; import { listservice } '../list/list.service'; @component({ selector: 'view', template: ` <div *ngif="!entity"> <p>select <b (click)="showrow()">row {{entity}}</b>!</p> </div> <div *ngif="entity"> <p >{{entity.id}}</p> <p >{{entity.name}}</p> <p >{{entity.weight}}</p> <p >{{entity.symbol}}</p> </div> `, styles: [] }) export class viewcomponent implements oninit { constructor( private route: activatedroute, private service: listservice ) { this.route.params.subscribe(params => { const id = parseint(params['id']); if (id) { const entity = this.service.getrow(id); this.entity = entity } }); } entity; showrow() { console.log(this.entity); } ngoninit() { } }
in this.entity
inside constructor have desired object when execute showrow this.entity
undefined, i'm doing wrong ? have tried change property different name, , didn't work expected, if 1 knows how resolve or point me right direction.
edit: getrow service
getrow(id) { console.log(id, 'test'); return this.datasource.find(row => row.id === id);//returns row }
move code ngoninit()
method , check value or not.
ngoninit() { this.route.params.subscribe(params => { const id = parseint(params['id']); if (id) { const entity = this.service.getrow(id); this.entity = entity } }); }
Comments
Post a Comment