|
| 1 | +// #docplaster |
| 2 | +// #docregion |
| 3 | +import 'package:angular2/core.dart'; |
| 4 | + |
| 5 | +import 'logger_service.dart'; |
| 6 | + |
| 7 | +////////////////// |
| 8 | +@Component( |
| 9 | + selector: 'my-child', |
| 10 | + template: '<input [(ngModel)]="hero">') |
| 11 | +class ChildComponent { |
| 12 | + String hero = 'Magneta'; |
| 13 | +} |
| 14 | + |
| 15 | +////////////////////// |
| 16 | +@Component( |
| 17 | + selector: 'after-content', |
| 18 | +// #docregion template |
| 19 | + template: ''' |
| 20 | + <div>-- projected content begins --</div> |
| 21 | + <ng-content></ng-content> |
| 22 | + <div>-- projected content ends --</div> |
| 23 | + <p *ngIf="comment != null" class="comment">{{comment}}</p> |
| 24 | + ''' |
| 25 | +// #enddocregion template |
| 26 | + ) |
| 27 | +// #docregion hooks |
| 28 | +class AfterContentComponent implements AfterContentChecked, AfterContentInit { |
| 29 | + String _prevHero = ''; |
| 30 | + String comment = ''; |
| 31 | + |
| 32 | + // Query for a CONTENT child of type `ChildComponent` |
| 33 | + @ContentChild(ChildComponent) ChildComponent contentChild; |
| 34 | + |
| 35 | +// #enddocregion hooks |
| 36 | + final LoggerService _logger; |
| 37 | + |
| 38 | + AfterContentComponent(this._logger) { |
| 39 | + _logIt('AfterContent constructor'); |
| 40 | + } |
| 41 | + |
| 42 | +// #docregion hooks |
| 43 | + ngAfterContentInit() { |
| 44 | + // contentChild is set after the content has been initialized |
| 45 | + _logIt('AfterContentInit'); |
| 46 | + _doSomething(); |
| 47 | + } |
| 48 | + |
| 49 | + ngAfterContentChecked() { |
| 50 | + // contentChild is updated after the content has been checked |
| 51 | + if (_prevHero == contentChild?.hero) { |
| 52 | + _logIt('AfterContentChecked (no change)'); |
| 53 | + } else { |
| 54 | + _prevHero = contentChild?.hero; |
| 55 | + _logIt('AfterContentChecked'); |
| 56 | + _doSomething(); |
| 57 | + } |
| 58 | + } |
| 59 | +// #enddocregion hooks |
| 60 | +// #docregion do-something |
| 61 | + /// This surrogate for real business logic; sets the `comment` |
| 62 | + void _doSomething() { |
| 63 | + comment = contentChild.hero.length > 10 ? "That's a long name" : ''; |
| 64 | + } |
| 65 | +// #enddocregion do-something |
| 66 | + |
| 67 | + void _logIt(String method) { |
| 68 | + var child = contentChild; |
| 69 | + var message = "${method}: ${child?.hero ?? 'no'} child content"; |
| 70 | + _logger.log(message); |
| 71 | + } |
| 72 | +// #docregion hooks |
| 73 | + // ... |
| 74 | +} |
| 75 | +// #enddocregion hooks |
| 76 | + |
| 77 | +////////////// |
| 78 | +@Component( |
| 79 | + selector: 'after-content-parent', |
| 80 | +// #docregion parent-template |
| 81 | + template: ''' |
| 82 | + <div class="parent"> |
| 83 | + <h2>AfterContent</h2> |
| 84 | +
|
| 85 | + <div *ngIf="show"> |
| 86 | + <after-content> |
| 87 | + <my-child></my-child> |
| 88 | + </after-content> |
| 89 | + </div> |
| 90 | +
|
| 91 | + <h4>-- AfterContent Logs --</h4> |
| 92 | + <p><button (click)="reset()">Reset</button></p> |
| 93 | + <div *ngFor="let msg of logs">{{msg}}</div> |
| 94 | + </div> |
| 95 | + ''', |
| 96 | +// #enddocregion parent-template |
| 97 | + styles: const ['.parent {background: burlywood}'], |
| 98 | + providers: const [LoggerService], |
| 99 | + directives: const [AfterContentComponent, ChildComponent]) |
| 100 | +class AfterContentParentComponent { |
| 101 | + final LoggerService _logger; |
| 102 | + bool show = true; |
| 103 | + |
| 104 | + AfterContentParentComponent(this._logger); |
| 105 | + |
| 106 | + List<String> get logs => _logger.logs; |
| 107 | + |
| 108 | + void reset() { |
| 109 | + logs.clear(); |
| 110 | + // quickly remove and reload AfterViewComponent which recreates it |
| 111 | + show = false; |
| 112 | + _logger.tick().then((_) { show = true; }); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments