angular-component-life-cycle

Angular ngFor lifecycle

泪湿孤枕 提交于 2019-12-20 03:17:00
问题 I'm using *ngFor to iterate some data like this: <div *ngFor="let d of [1, 2, 3]"> {{d}} </div> Everything is fine and I got the result as expected: <div> 1 </div> <div> 2 </div> <div> 3 </div> Then, I wondered if I use a function instead of {{d}} , so I wrote a function: protected logAndReturn(item) { console.log(item); return item; } and I used it: <div *ngFor="let d of [1, 2, 3]"> {{logAndReturn(d)}} </div> I got the same rendered HTML result as the first code, but console output wasn't my

Angular ngFor lifecycle

偶尔善良 提交于 2019-12-01 23:01:10
I'm using *ngFor to iterate some data like this: <div *ngFor="let d of [1, 2, 3]"> {{d}} </div> Everything is fine and I got the result as expected: <div> 1 </div> <div> 2 </div> <div> 3 </div> Then, I wondered if I use a function instead of {{d}} , so I wrote a function: protected logAndReturn(item) { console.log(item); return item; } and I used it: <div *ngFor="let d of [1, 2, 3]"> {{logAndReturn(d)}} </div> I got the same rendered HTML result as the first code, but console output wasn't my expected output. Console output: 1 2 3 1 2 3 Angular is running in the development mode. Call

Angular/RxJs When should I unsubscribe from `Subscription`

Deadly 提交于 2019-11-25 21:41:02
问题 When should I store the Subscription instances and invoke unsubscribe() during the NgOnDestroy life cycle and when can I simply ignore them? Saving all subscriptions introduces a lot of mess into component code. HTTP Client Guide ignore subscriptions like this: getHeroes() { this.heroService.getHeroes() .subscribe( heroes => this.heroes = heroes, error => this.errorMessage = <any>error); } In the same time Route & Navigation Guide says that: Eventually, we\'ll navigate somewhere else. The