|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { Hero } from './hero.model'; |
| 3 | +import { HeroModel } from './onpush/hero-evented.model'; |
| 4 | + |
| 5 | +@Component({ |
| 6 | + moduleId: module.id, |
| 7 | + selector: 'hero-app', |
| 8 | + template: ` |
| 9 | + <h1>Angular Change Detection</h1> |
| 10 | +
|
| 11 | +
|
| 12 | + <h2>Basic Example</h2> |
| 13 | + <hero-counter> |
| 14 | + </hero-counter> |
| 15 | +
|
| 16 | + <h2>Single-Pass</h2> |
| 17 | +
|
| 18 | + <h3>Broken Example</h3> |
| 19 | + <hero-name-badge-broken [hero]="anonymousHero"> |
| 20 | + </hero-name-badge-broken> |
| 21 | +
|
| 22 | + <h3>Fixed Example</h3> |
| 23 | + <hero-name-badge [hero]="secondAnonymousHero"> |
| 24 | + </hero-name-badge> |
| 25 | +
|
| 26 | +
|
| 27 | + <h2>OnPush</h2> |
| 28 | +
|
| 29 | + <h3>Immutable Primitive Values</h3> |
| 30 | + <p>OnPush only runs detection when inputs change.</p> |
| 31 | + <hero-search-result [searchResult]="'Windstorm'" [searchTerm]="'indsto'"> |
| 32 | + </hero-search-result> |
| 33 | +
|
| 34 | + <h3>Mutable Collection, Broken Example</h3> |
| 35 | + <p>OnPush does not detect changes inside array inputs.</p> |
| 36 | + <hero-manager-mutable> |
| 37 | + </hero-manager-mutable> |
| 38 | +
|
| 39 | + <h3>Immutable Collection, Fixed Example</h3> |
| 40 | + <p>OnPush detects changes for array inputs as longs as they're treated as immutable values.</p> |
| 41 | + <hero-manager-immutable> |
| 42 | + </hero-manager-immutable> |
| 43 | +
|
| 44 | + <h3>Events</h3> |
| 45 | + <p>OnPush detects changes when they originate in an event handler.</p> |
| 46 | + <hero-counter-onpush> |
| 47 | + </hero-counter-onpush> |
| 48 | +
|
| 49 | + <h3>Explicit Change Marking, Broken Without</h3> |
| 50 | + <p>A counter incrementing with setTimeout() inside an OnPush component does not update.</p> |
| 51 | + <hero-counter-auto-broken> |
| 52 | + </hero-counter-auto-broken> |
| 53 | +
|
| 54 | + <h3>Explicit Change Marking</h3> |
| 55 | + <p>This is fixed using markForCheck()</p> |
| 56 | + <hero-counter-auto> |
| 57 | + </hero-counter-auto> |
| 58 | +
|
| 59 | + <h3>Explicit Change Marking with Library Callback</h3> |
| 60 | + <hero-name-badge-evented [hero]="heroModel"> |
| 61 | + </hero-name-badge-evented> |
| 62 | + <button (click)="renameHeroModel()">Rename</button> |
| 63 | +
|
| 64 | + <h2>Detaching</h2> |
| 65 | +
|
| 66 | + <h3>Permanently, "One-Time Binding"</h3> |
| 67 | + <p>By detaching a component's change detector at ngOnInit() we can do "one-time binding".</p> |
| 68 | + <hero-name-badge-detached [hero]="hero"> |
| 69 | + </hero-name-badge-detached> |
| 70 | + <button (click)="renameHero()">Rename</button> |
| 71 | +
|
| 72 | + <h3>Temporarily, reattach</h3> |
| 73 | + <p>By detaching/reattaching a change detector we can toggle whether a component has "live updates".</p> |
| 74 | + <hero-counter-live> |
| 75 | + </hero-counter-live> |
| 76 | +
|
| 77 | + <h3>Throttling with Internal detectChanges</h3> |
| 78 | + <p> |
| 79 | + By calling detectChanges() on a detached change detector we can choose when change detection is done. |
| 80 | + This can be used to update the view at a lower frequency than data changes. |
| 81 | + </p> |
| 82 | + <hero-counter-throttled> |
| 83 | + </hero-counter-throttled> |
| 84 | +
|
| 85 | + <h3>Flushing to DOM with Internal detectChanges</h3> |
| 86 | + <p>We can use detectChanges() to flush changes to the view immediately if we can't wait for the next turn of the zone.</p> |
| 87 | + <hero-signature-form> |
| 88 | + </hero-signature-form> |
| 89 | +
|
| 90 | + <h2>Escaping NgZone For Async Work</h2> |
| 91 | +
|
| 92 | + <h3>Without</h3> |
| 93 | + <p>Many unnecessary change detections will be performed for this workflow because it is all inside NgZone.</p> |
| 94 | + <hero-async-workflow></hero-async-workflow> |
| 95 | + ` |
| 96 | +}) |
| 97 | +export class AppComponent { |
| 98 | + hero: Hero = {name: 'Windstorm', onDuty: true}; |
| 99 | + anonymousHero: Hero = {name: '', onDuty: false}; |
| 100 | + secondAnonymousHero: Hero = {name: '', onDuty: false}; |
| 101 | + |
| 102 | + heroModel = new HeroModel('Windstorm'); |
| 103 | + |
| 104 | + renameHero() { |
| 105 | + this.hero.name = 'Magneta'; |
| 106 | + } |
| 107 | + |
| 108 | + renameHeroModel() { |
| 109 | + this.heroModel.setName('Magneta'); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments