Angularãžã®ç§»è¡ã«é¢ããã¹ããŒãªãŒã®æåŸã®éšåã§ã¯ãéçºè
ãæ°ãããã¬ãŒã ã¯ãŒã¯ã«æ
£ããã®ã«åœ¹ç«ã€å
éšããã¥ã¡ã³ãã®éžæãããå Žæãå
±æããŸãã æ°ããã³ã³ããŒãã³ãã³ã³ãã€ã«ããžãã¯ã®æ©èœãå€æŽæ€åºãããã³ãã©ã³ã¹ã¯ã«ãŒãžã§ã³ã®æŠå¿µã«ã€ããŠèª¬æããŸãã ãããã¯ãAngularã§äœæ¥ãããšãã«çŸåšäœ¿çšãããŠããçŸåšã®èŠåã§ãã ããŠãæåŸã«-ååã«ããããããè±èªã®èšäºããããªãžã®ãªã³ã¯ã
åã®ã·ãªãŒãºïŒ æåã¯ç§»è¡ã®æºåã«ã€ã㊠ã 2çªç®ã¯ãã€ããªããã¢ãŒãã§äœæ¥ããæ©èœã«ã€ããŠã§ã ã
ã¢ããªã±ãŒã·ã§ã³ãšAoTã®ã³ã³ãã€ã«
Angularã®äž»ãªå€æŽç¹ã®1ã€ã¯ãã³ã³ããŒãã³ãã®ã³ã³ãã€ã«ããžãã¯ã§ãã
AngularJSã§ã¯ãæåã®ã³ã³ããŒãã³ãïŒäžçªäžã®ã³ã³ããŒãã³ãïŒã®HTMLãååŸããDOMã«è©°ãããã©ãŠã¶ãŒãããã解æããããDOMãã©ã°ã¡ã³ããæ§ç¯ããããDOMã«æ¿å
¥ããããåä¿¡ããDOMãã©ã°ã¡ã³ããžã®ãªã³ã¯ãååŸãããã®äžã®è§åœ¢éšåã解æããŸãïŒãã£ã¬ã¯ãã£ãïŒ ãã³ã³ããŒãã³ãïŒãèŠã€ãã£ãåã³ã³ããŒãã³ãã«å¯ŸããŠç¹°ãè¿ãããŸããã
Angularã¯ãjs / tsã®ãã³ãã¬ãŒãã®ã³ã³ãã€ã«ã2ã€ã®ããªã¢ã³ãïŒJiTããã³AoTïŒã§äœ¿çšããŸãã ã¢ããªã±ãŒã·ã§ã³ã®éå§æã«ïŒJiTã¢ãŒãã§ïŒããã¬ãŒã ã¯HTMLãå«ãæååãããã¹ãŠã®ã³ã³ããŒãã³ãã®ãã¹ãŠã®ïŒäžè¬çã«ãã¹ãŠã®ïŒãã³ãã¬ãŒããååŸããŠã³ã³ãã€ã«ããç®çã®DOMããŒã¹ãäœæããŸãã ã€ãŸã DOMãã©ã°ã¡ã³ãã®è§£æãšæ§ç¯ã¯ãã©ãŠã¶ã«ãŸã£ããäŸåãããAngularã¯èªåã§è¡ããŸãã ãã®åŸãæäžäœã®ã³ã³ããŒãã³ãããéå§ããŠã察å¿ããæ¢è£œã®JSã³ãŒãã®å®è¡ãéå§ããŸããããã«ãããDOMã®æçãäœæãããããªãŒå
ã®ç®çã®å Žæã«ããã«æ¿å
¥ãããŸãã æ¿å
¥åŸãè¿œå ã®è§£æã¯å¿
èŠãããŸããã ã³ã³ãã€ã©ã¯ãã§ã«ãã¹ãŠã解æããŠããŸãã
AoTã³ã³ãã€ã«ã¯ãã¢ã»ã³ããªäžã«ãµãŒããŒäžã§ããŒã«ã«ã«å®è¡ãããŸããå°ãæéãããããŸãããåºåã¯TSãã¡ã€ã«ã®åœ¢åŒã§å¥ã®ãã£ã¬ã¯ããªã«ãããã¹ãŠã®ã³ã³ããŒãã³ãã®ããŒã¿ãæäŸããŸãã ãããã¯ã¡ã€ã³ã¢ããªã±ãŒã·ã§ã³ã«ããã¿ã€ããTypeScriptã®ã³ã³ãã€ã«ã¯ãã§ã«ã³ããŒããããã³ãã¬ãŒããèæ
®ããŠããŸãã
AoTã¢ãŒãã§ã¯ããã³ãã¬ãŒãã¯JSã§ã¯ãªãTypeScriptã§ã³ã³ãã€ã«ãããŸãã ããã«ããããã¹ãŠã®ãã¿ãŒã³ã®éçãªåæå®ãå¯èœã«ãªããŸãã å€æ°ãã¹ããããããã©ã€ããŒããšããŠæå®ããããäžé©åã«åŠçããŸããïŒæååãåŸ
æ©ããŠããã³ã³ããŒãã³ãã®å
¥åã«ãªããžã§ã¯ããæŒã蟌ãããšããïŒ-ã¢ã»ã³ããªäžã«ãšã©ãŒãçºçããŸããã ãã ããããã¯å®çšŒåçšã®ãã«ãã¢ãŒãã®ã¿ã§ãã 倧éã®åä»ãã³ãŒããè¿œå ããããããã¢ã»ã³ããªïŒã³ã³ãã€ã«ts-> jsïŒãå°ãé
ããªããéçºäžã«AoTã¢ãŒãããªãã«ãªããŸãïŒãã«ãã¹ã¬ããã³ã³ãã€ã«ã®æ¥œããæããAoTãéçºã¢ãŒãã§äœ¿çšããããã®èŠåŽãåŸ
ã£ãŠããŸãïŒã
ãããã¯ã«é¢ãããªã³ã¯ïŒ first ã secondããã³third ã
å€æŽæ€åºïŒCDïŒ
Angularã«ã¯ã°ããŒãã«ãã€ãžã§ã¹ããµã€ã¯ã«ïŒããã³å€æŽå¯èœãªãã©ã¡ãŒã¿ã®ãªã¹ããããããã§ãã¯ããå¿
èŠããããŸãïŒããªããªãããã€ãã£ãã€ãã³ããã€ã³ãã£ã³ã°ïŒ addEventListener
ãªã©ïŒãæ倧éã«äœ¿çšãããŸãããæ€åºãšã³ãžã³èªäœã¯æ¶ããŠããŸããã
åå¥ã®ã€ãã³ããã£ã¬ã¯ãã£ããšã¿ã€ã ã¢ãŠã/ééã®ç¹å¥ãªãµãŒãã¹ãªãã§äœããèµ·ãã£ãããšãAngularã¯ã©ã®ããã«ç¥ã£ãŠããŸããïŒ
Angularã®å¥åã®ããŒãžã§ã³ãããDartèšèªããããŸãŒã³ã®æçšãªæŠå¿µãåçšããŸãããããã«ãããç¹å®ã®ãŸãŒã³ã§ã³ãŒãã®å®è¡ãéå§ãããã¿ã€ãã³ã°ãšãéåæåŒã³åºããèæ
®ããŠçµäºããã¿ã€ãã³ã°ãç¥ãããšãã§ããŸãã ããã¯ãã¹ãŠzone.js libã§åœ¢ã«ãªããŸãã ãããã«ããããã®ãããªãŸãŒã³ãäœæ/æäœã§ããAngularãšã³ãžã³å
ã§ç©æ¥µçã«äœ¿çšãããŸãã éåæã€ãã³ãã«é
ããªãããã«ããããã«ãzone.jsã¯å¯Ÿå¿ããã¡ãœãã-addEventListenerã setTimeout
ã setInterval
ã Promise
ã¡ãœãããªã©ããã³ã€ïŒãã®ããŒãžã§ã³ã«çœ®ãæãïŒãsetInterval
ã ããã«ãããã€ãã³ã/è¿œå ãµãŒãã¹ã«ãã€ã³ãããããã®åå¥ã®ãã£ã¬ã¯ãã£ããäžèŠã«ãªããŸããã ãããã£ãŠãzone.jsã§ãã£ãããããã€ãã³ãã®æåŸã«ãAngularã¯å€æŽæ€åºããã«ããŠãã¢ãã«ãDOMãšåæããŸãã éçºã¢ãŒãã§ã¯ãæ€åºã¯2åäœåããŸãã2åç®ã¯ã1åç®ä»¥éã¯äœãå€æŽãããŠããªãããšã確èªããããå€æŽãããå Žåã¯ïŒã³ã³ãœãŒã«ã®ãšã©ãŒïŒãäžããŸãã æ¬çªç°å¢ã§ã¯ãåžžã«1åã ãã§ãïŒãã€ãžã§ã¹ããµã€ã¯ã«ãšå¯Ÿå¿ãããšã©ãŒã®åŒã³åºãã10åã«ãªããŸããïŒã
ãã§ãã¯ããããã©ã¡ãŒã¿ã®ã°ããŒãã«ããŒã«ããªãå Žåãå€æŽæ€åºã¯ã©ã®ããã«è¡ãããŸãã
ããã¯ãã³ã³ããŒãã³ãã®äœæ¥ã§ãã ããããã«ãDOMãŸãã¯ç¹å¥ãªãã€ã³ãã£ã³ã°ïŒ HostBinding
ãªã©ïŒã§äœããã®åœ¢ã§äœ¿çšããããã©ã¡ãŒã¿ãŒã®ã»ãããHostBinding
ããšãã§ããŸãã ãã®ãããªãã©ã¡ãŒã¿ã¯ãã³ã³ããŒãã³ãå
ã®åå¥ã®ãªã¹ãã«æ ŒçŽãããŸãïŒä»ã®ã©ãã«ãç¥ãããŠããªãïŒã
Angularãã°ããŒãã«æ€åºå€æŽãã°ãååŸãããšãåã³ã³ããŒãã³ãïŒããªãŒã®äžéšããäžéšïŒèªäœããã©ã¡ãŒã¿ãŒã®ãã§ãã¯ã«é¢äžããŸãã åæã«ãå€æŽæ€åºã¡ã«ããºã ãã³ã³ããŒãã³ãããåãé¢ãããšãã§ãããã®ãã©ã¡ãŒã¿ãŒã¯ååãšããŠãã§ãã¯ãããŸããã ãŸãã¯ãå
¥åã®å€æŽã®ã¿ããã§ãã¯ããããã«æ§æããããšãã§ãïŒ ChangeDetectionStrategy.OnPush
ïŒãäœããã§ãã¯ããªãã ãã§ãªããããã«å«ãŸããã³ã³ããŒãã³ã/ãã£ã¬ã¯ãã£ãã®ãã§ãã¯ã®éå§ãå®å
šã«é®æããŸãã ãããã£ãŠã OnPush
ãæ£ããåæ£ãããŠãããããåãã£ãã¯ã®ã¹ãã£ã³ããã³ã³ããŒãã³ãã®ãã©ã³ãå
šäœãé€å€ã§ããŸãã
ãŸããAngularãŸãŒã³ã®å€éšãŸãã¯ïŒæ瀺çã«ïŒã³ãŒãã®äžéšãå®è¡ããããšãã§ããŸãïŒAngularãŸãŒã³ã§ã¯ãã€ãã³ããªã©ããã£ããããŸãïŒã ããã¯ãããããã¹ãŠã®ãã©ãã«ãããŸããããã«ãµãŒãããŒãã£è£œã®ã©ã€ãã©ãªã«åºå·ããå Žåã«åœ¹ç«ã¡ãŸãã ãŸããã©ã€ãã©ãªå
ã®ãã¹ãŠã®ã€ãã³ããã¿ã€ã ã¢ãŠãã«å¯ŸããŠãã³ã³ããŒãã³ãããªãŒå
šäœã§ãããããçºçããããšã¯ãããŸããã åŸã§ããŒã¿ãæŽæ°ããããšãè§åºŠã«äŒããããã«ïŒåœŒãèããŠãããŸãŒã³ã®å€éšã§èµ·åããããã®libããååŸïŒãè§åºŠãŸãŒã³ã§äœããã®ã¢ã¯ã·ã§ã³ïŒããšãã°ãã³ã³ããŒãã³ãã®ããŒã«ã«ããããã£ã®å€æŽïŒã®å®è¡ãæ瀺çã«èŠå®ããŸãã
detectChangesããã³markForCheck
åé¢ãããCDãŸãã¯ã³ã³ããŒãã³ãïŒããªãŒã®çŸåšã®ã³ã³ããŒãã³ã以äžïŒã«ChangeDetectionStrategy.OnPush
ã ChangeDetectionStrategy.OnPush
æžã蟌ãŸããèªåçã«ååŸãããªãå€æŽãããå ŽåãããŒã«ã«ã®ChangeDetectorRef
ãµãŒãã¹ã«ã¯2ã€ã®ã¡ãœããããããŸãdetectChanges
ããã³markForCheck
ïŒ
markForCheck
ïŒçŸåšã®ã³ã³ããŒãã³ãïŒå
æ¬çïŒããããªãŒã®ã«ãŒãã³ã³ããŒãã³ããŸã§ããã¹ãŠã®ãã¥ãŒãã°ããŒãã«CDã®æãè¿ããã£ãã¯ã®ãã§ãã¯ãå¿
èŠã§ãããšããŠããŒã¯ããŸãã ãã£ãã¯èªäœã¯åæã«ãããããããä»ã®èª°ãããã®ãã£ãã¯ããã«ããå ŽåïŒãŸãã¯æ¢ã«ãã«ãããŠããããå€æŽãå®å®ããã®ãåŸ
ã£ãŠããå ŽåïŒã«ã®ã¿ãã¥ãŒããã§ãã¯ãããŸããdetectChanges
ïŒæ€åºåš/å
¥åã®ç¶æ
ã«é¢ä¿ãªããçŸåšã®ã³ã³ããŒãã³ãã®ãã¥ãŒã«å¯ŸããŠã®ã¿CDãåŒãåºããŸãã ãã¹ãŠã®åã«ã€ããŠãæšæºããžãã¯ãæ©èœããŸãOnPush
ãããå Žåãå
¥åã確èªããå€æŽæ€åºåšãåæãããŠããå Žåãäœãç£èŠãããç¡èŠããŸãã äœæ¥ã®ç¹åŸŽ-ã³ã³ããŒãã³ããç Žå£ã®éçšã«ããïŒãŸãã¯ç Žå£ãããŠãããšèŠãªãããïŒå Žåããã®ãã¥ãŒãåæããããšã detectChanges
åŒã³åºãdetectChanges
ãã®ç¬éã«ãšã©ãŒdetectChanges
ã¹ããŒããŸãïŒããšãã°ãã¿ã€ã ã¢ãŠãã§ãã«ããããšã決å®ããã³ã³ããŒãã³ãã¯ã«ãŒãã®å€æŽã«ãããã§ã«ç Žå£ãããCDããã«ããŸãïŒååšããªããã¥ãŒã§ïŒã glyãããã¯-æ¢ã«ãã«ããããã¥ãŒã確èªããŸãïŒãã®ãããªå Žåã¯é¿ããŠãã ããïŒïŒ
if (!this.changeDetectorRef["destroyed"]) { this.changeDetectorRef.detectChanges(); }
䜿çšããå ŽåïŒ
OnPush
ãåããã³ã³ããŒãã³ãããããå€æŽããåæ³çãªãæ¹æ³ã§è¡ãããå ŽåïŒapiãããã¿ã€ã ã¢ãŠããã°ããŒãã«CDã®ç®çãã®ãããŸãŒã³å
ã§å¥œããªããã«ïŒã markForCheck
䜿çšãmarkForCheck
ã- ã³ã³ããŒãã³ãã«ãã¿ãããããCDãããå ŽåããŸãã¯äœããè§åºŠãŸãŒã³ã®å€åŽã§å€æŽãããå ŽåïŒæããã«ã°ããŒãã«CDãåŒã£åŒµã£ã人ãããªãå ŽåïŒã
detectChanges
ã䜿çšããã³ã³ããŒãã³ããç Žå£ãããå¯èœæ§ãããããšãå¿ããªãã§ãã ããã
ng-content
- ng-transclude
ãšåãã§ã¯ãããŸãã
Angularã®ãã©ã³ã¹ã¯ã©ããžã®æŠå¿µã¯å°ãå€æŽãããŸããïŒãããWebã³ã³ããŒãã³ãã§ã©ã®ããã«è¡ãããããããã³ãšã³ãžã³/ã³ã³ãã€ã©ã®æ©èœã®åœ±é¿ãåããŠïŒã å€éšã§ã¯ã ng-content
ã¯ng-transclude
ãšåãæ©èœãæã¡ãã³ã³ããŒãã³ãã«äœããæããŠãç¹å®ã®å Žæã«æãselect
ïŒå±æ§ãselect
ãŸãïŒã
ãã ãã1ã€ã®å°ããªéãããããŸãng-content
contentå
ã«ããã©ã«ãã®ã³ã³ãã³ããæå®ããæ¹æ³ã¯ãããŸãã-éåžžã«éèŠãªãã®ã§ãïŒãã®ã³ã³ãã³ããã¹ããŒãã人ã¯ã ng-content
ã¿ã°ãå«ãã³ã³ããŒãã³ãã§ã¯ãªãã ng-content
ããŒã«ãããã³ã³ãã³ãã®åæåãæ
åœããŸãã
äŸã®èª¬æïŒ
@Component({ ... template: ` Visible: {{ visible }} <ng-content *ngIf="visible"></ng-content> `, }) class ProjectionComponent implements OnInit { public visible = false; public ngOnInit() { window.setTimeout(() => this.visible = true, 1000); window.setTimeout(() => this.visible = false, 2000); } } @Component({...}) class ChildComponent implements OnInit, AfterViewInit, OnDestroy { public ngOnInit() { console.log("Child component ngOnInit"); } public ngAfterViewInit() { console.log("Child component ngAfterViewInit"); } public ngOnDestroy() { console.log("Child component ngOnDestroy"); } } @Component({ ... template: ` <vim-projection> <vim-child-component></vim-child-component> </vim-projection> `, }) class ParentComponent {}
ProjectionComponent
ãšã³ã³ãœãŒã«ãã°ã§ngIf
ïŒ
// `Visible` `ProjectionComponent` Child component ngOnInit // Child component ngAfterViewInit // 2 // âŠ
æåã®1ç§ã§ã¯ã *ngIf="false"
ã«ããããããã ChildComponent
ãåæåããïŒ OnInit
ããã¯ãæ©èœïŒã OnDestroy
ã¯2ç§åŸã«OnDestroy
ããŸããã§ããããã³ã³ããŒãã³ãã¯é衚瀺ã«ãªã£ãããã§ãã
ã€ãŸã ng-content
ãå«ãã³ã³ããŒãã³ãã§äœãèµ·ãã£ãŠããããã«ã¹ããŒããããã®ã¯ãã¹ãŠåæåãããŸãïŒ OnInit
ããã¯ãåŒã³åºãããŸãïŒã ããã¯ãã³ã³ããŒãã³ãã®åæåã§èŠæ±ãéãåŠçãªã©ãããå Žåã«éèŠã§ãã
ãã®ãããªã³ã³ãã³ããé衚瀺ã«ããå¿
èŠãããå Žåã¯ããããã¹ããŒãã人ããããè¡ãå¿
èŠããããŸãã
<vim-projection> <vim-child-component *ngIf="visibleInParent"></vim-child-component> </vim-projection>
ãŸãã¯ãTemplateRefã䜿çšããŠãã®å¶éãåé¿ããèå³æ·±ãæ¹æ³ã䜿çšããŸãã
<div *ngIf="condition" class="smth1"> <ng-container *ngTemplateOutlet="contentTpl"></ng-container> </div> <div *ngIf="!condition" class="smth2"> <ng-container *ngTemplateOutlet="contentTpl"></ng-container> </div> <div *ngIf="condition2" class="smth1"> <ng-container *ngTemplateOutlet="contentWithSelectorTpl"></ng-container> </div> <div *ngIf="!condition2" class="smth2"> <ng-container *ngTemplateOutlet="contentWithSelectorTpl"></ng-container> </div> <ng-template #contentTpl><ng-content></ng-content></ng-template> <ng-template #contentWithSelectorTpl><ng-content select="[some-attribute]"></ng-content></ng-template>
å¶éäºé
-ãã®ãããªng-content
ãäžåºŠã«æ¿å
¥ã§ããã®ã¯1ç®æã®ã¿ã§ãã
ãŸãã ng-content
ã§ã¯ããã³ãã¬ãŒãã«è€æ°ã®åäžã®ã¹ããããæå®ããããšã¯ã§ããŸãããåãã¹ãããã衚瀺ãããšãæ¡ä»¶ããã³ã°ããŸãã
<div *ngIf="smth" class="smth1"> <ng-content></ng-content> </div> <div *ngIf="!smth" class="smth2"> <ng-content></ng-content> </div> <div *ngIf="smth" class="smth1"> <ng-content select="[some-selector]"></ng-content> </div> <div *ngIf="!smth" class="smth2"> <ng-content select="[some-selector]"></ng-content> </div>
æçšãªè³æãžã®ãªã³ã¯
Googleã§ã¯ãè§åºŠ2+ã«å€ãã®ããšããããŸããããã«/ããã¯ã§äžæãªãããã¯ãå®å
šã«æ€çŽ¢ã§ããŸãã ãªãªãŒã¹ããïŒãããŠãã以åïŒäººã
ã¯ãã¬ãŒã ãããŒãã«å解ãããããã©ã®ããã«æ©èœããããç解ããŠããŸããã
䟿å©ãªãªã³ã¯ã次ã«ç€ºããŸãã
ElementRefãTemplateRefãViewContainerRef
æ§é ãã£ã¬ã¯ãã£ã1
æ§é ãã£ã¬ã¯ãã£ã2
ViewChildrenãContentChildrenãQueryList
åçã³ã³ããŒãã³ã1
åçã³ã³ããŒãã³ã2
å€æŽæ€åº
ng-templateãšæ§é ãã£ã¬ã¯ãã£ãã®ãã€ã¯ãæ§æ
ãããŠæåŸã«ããã€ãã®ããã«ã Angularã ãã§ãªãã¯ãŒã«ãªéçºè
ãåžžã«æ¢ããŠããããšãæãåºãããŠãã ããã