Webéçºã®äžçã§ã¯ã Angular 2ã®ããŒã¿çããªãªãŒã¹ãããŸãã ã ãŸãããªãªãŒã¹åŸã«ã©ã®ããã«èŠãããã«ã€ããŠã¯ããã§ã«æ³å®ããããšãã§ããŸãã
ããããè©äŸ¡èªäœã¯ãç空äžã§ã補é æ¥è ã®ãã³ãã¬ããã«ããé»åæ©åšã®éžæã«äŒŒãŠããŸãã ã¬ãžã§ããã«ãããã¹ãŠã®ããããèšèŒãããŠããŸãã ãããŠãããã«ãªããã®ã¯ãã¹ãŠèšåãããŠãããããããã®ååšã«ã€ããŠèããããšããã§ããªããããããŸããã ãããã£ãŠãä»ã®äœããšæ¯èŒããŠè©äŸ¡ããæ¹ãã¯ããã«å¹ççã§ãã
ãã®ãããAngular 2ãšæ°ããããããéåžžã«éå¿çãªãããžã§ã¯ãã§ããAureliaãæ¯èŒãããšããã¢ã€ãã¢ãçãŸããŸããã åæã«ãHabrã®è²¯éç®±ã«ãã®ãã¬ãŒã ã¯ãŒã¯ã«é¢ããæ å ±ãè£å ããŸããããã¯ããããŸã§ã®ãšãããAngular 2ã«é¢ããæ å ±ãããã¯ããã«å°ãªãããã§ãã
ããŒã³ãã®æºåãã§ãããããžã§ã¯ãã®åœ¢åŒã®èšäºã®äŸã®ã³ãŒãã¯githubã«æçš¿ãããŠããŸãã äŸã¯TypeScriptã§èšè¿°ãããŠãããã©ã¡ããsystemjsã䜿çšããŠããŸã ã systemjsã®æ§æã¯ãåãã¬ãŒã ã¯ãŒã¯ã®ã¯ã€ãã¯ã¹ã¿ãŒãã¬ã€ãããåããããã®ã§ããããŸã£ããç°ãªããŸãã ãã¬ãŒã ã¯ãŒã¯ã®äœè ãæäŸãã圢ã®ãŸãŸã«ããŠããããšã¯åççã§ããããã«æããã䌌ããããªãã®ã«ããããšããªãã§ãã ããã ãŸãããããžã§ã¯ãã§äœ¿çšãããŠããhttpãµãŒããŒã¯pushStateããµããŒãããŠããªãããšã«æ³šæããŠãã ããïŒæ¿èªæžã¿ã®ãã«ãªã¯ãšã¹ãããããããããã«ãµããŒããããããšãé¡ã£ãŠããŸãïŒããããã£ãŠãAngularã«ããã·ã¥ããŒã¹ã®ã«ãŒãã£ã³ã°ãå«ããå¿ èŠããããŸããã
ãã¹ãŠã®ã¢ãŒããã¯ãã£æ©èœã培åºçã«æ¯èŒããã®ã§ã¯ãªãããã¬ãŒã ã¯ãŒã¯ãè©äŸ¡ããã®ã¯ããšã³ããŠãŒã¶ãŒãã©ã®ãããªã¡ãªãããåŸãããç解ããããšãã芳ç¹ããã§ãã 1ã€ã®èšäºã§ãã¹ãŠãæ¯èŒããŠãæ©èœããŸããããã®ããããã®èšäºã¯äžè¬çãªæ§é ã«ç²ŸéããŠããŸãã ãããŠãããé£ãã質åã¯ç¬¬2éšã«æ®ããŸãã
次ã®ã·ããªãªã«åŸããŸãã
- Aureliaã®æŠèŠãšããããAngularãšæ¯èŒããããšãæ£åœã§ããçç±ãåŠã¶
- åèªç©ºåçã®é·æãšçæãæ€èšãã
- æ©èœãçšèªãä¿åãããã®ãæå®ããŸããã
- åãã¬ãŒã ã¯ãŒã¯ã§æãã·ã³ãã«ãªã³ã³ããŒãã³ããäœæããŸããã
- ã«ãŒãã£ã³ã°ãæ§æãã
- ãã¹ããããã³ã³ããŒãã³ããè¿œå ãã
- æãåçŽãªããŒã¿ãã€ã³ãã£ã³ã°ãªãã·ã§ã³ãæ€èšãã
- ããŒã¿ãã€ã³ãã£ã³ã°ã§å¶åŸ¡æ§é ãèæ ®ãã
ã¢ãŠã¬ãªã¢ãšã¯äœã§ããïŒãªãAngularãšæ¯èŒããã®ãæ£åœãªã®ã§ããïŒ
åãHabréã®Aureliaã«ã€ããŠã®æ å ±ã¯ã»ãšãã©ãªãããããã¹ãŠã®èªè ãããã«ã€ããŠèããããã§ã¯ãªããšæ³å®ã§ããŸãã ã§ãããããªã¬ãªã¢ã®èªçã®èæ¯ãã話ãããŸãã ããã«ãããã¯éåžžã«èå³æ·±ããå°ãåçã§ãããããŸãã
Aureliaã¯ã Caliburn.Micro XAMLãã©ãããã©ãŒã åãã®éåžžã«äººæ°ã®ããMV *ãã¬ãŒã ã¯ãŒã¯ã®äœæè ã§ããRob Eisenbergã®ãããžã§ã¯ãã§ãã 圌ã¯åŸã«DurandalãšåŒã°ããWebçšã®MV *ãã¬ãŒã ã¯ãŒã¯ãéçºããŸããã ãã¥ã©ã³ãã«ã¯ããŸã人æ°ããããŸããã§ããããããã§ãéåžžã«èå³æ·±ããšã¬ã¬ã³ããªãœãªã¥ãŒã·ã§ã³ãããããã¬ãŒã ã¯ãŒã¯ã¯ãããéåžžã«æãããã©ãã¯ãŒã®èŽè¡ãéããŸããã
ããããRob Eisenbergã¯Durandalã®ãã¹ãŠã®æ¬ ç¹ãç解ããŠããããã圌ã®ãµããŒããšãšãã«ã ããããNextGenãã¬ãŒã ã¯ãŒã¯ã®éçºã«æºãããŸããã
2014幎1æãngConfã«ã³ãã¡ã¬ã³ã¹ã§ã John Papaã¯Webéçºã®äžçã§ããç¥ãããDangrandalãšNextGenãã¬ãŒã ã¯ãŒã¯ã§Rob Eisenbergã«ãã£ãŠææ¡ãããã¢ã€ãã¢ãAngularããŒã ãããŒãžã£ãŒBrad Greenãšå ±æããŸããã ãããã®ã¢ã€ãã¢ã¯ã°ãªãŒã³ã«èå³ããããã¢ã€ãŒã³ããŒã°ãšè©±ãããããšã«ããŸããã
äŒã£ãåŸããã©ããã»ã°ãªãŒã³ãšããã»ã¢ã€ãŒã³ããŒã°ã¯ããŠã§ããšãŠã§ãéçºã®æªæ¥ã«å¯Ÿãã圌ãã®èŠè§£ãå€ãã®ç¹ã§äžèŽããŠããããšã«æ°ã¥ãã圌ãã¯åãåãããããšã«ããã¢ã€ãŒã³ããŒã°ã¯ãã¬ãŒã ã¯ãŒã¯ã®ç¬¬2ããŒãžã§ã³ã§AngularããŒã ã§åãå§ããŸãã ã
ãããã10ãæåŸã圌ã¯Angular 2ã®éçºã®æ¹åæ§ã倧ããå€ãããããŠã圌ãååããããšã«åæããAngular 2ãšã¯ç°ãªããããAngularããŒã ãèŸããããšã«ããŸããã
ã¢ã€ãŒã³ããŒã°ã¯çæéã§ãããšãã°ã¹ã³ããã»ã¢ã¬ã³ãªã©ã®ã¹ã¿ãŒãå«ãããªã倧ããªããŒã ãç·šæãã倢ã®æ çµã¿ã®ããšã«æ»ã£ãŠããŸããã ãã®ä»äºã®çµæã¯ã¢ãŠã¬ãªã¢ã§ããã
äžè¬ã®äººã ã¯ãã®ãã¬ãŒã ã¯ãŒã¯ãèå³ãæã£ãŠåãå ¥ããŸããïŒè©äŸ¡æç¹ã§æãç°¡åãªæ¹æ³ãšããŠãAureliaã¯githubã§5,000åã®æãåéããŸããããAngular 2ã§ã¯8,000 åã®æãåéããŸããïŒã
Angular 2ãšAureliaã§èŠå®ãããŠããäžè¬çãªååã¯éåžžã«äŒŒãŠãããåãã¯ã©ã¹ã®ãã¬ã€ã€ãŒã§ãã ãããã詳现ãªããžã§ã³ãšåœŒããæã£ãŠããæ©èœã®ã»ããã¯ãŸã£ããç°ãªã£ãŠãããããæ¯èŒãèå³æ·±ããã®ã«ãªã£ãŠããŸãã
Angular 2ãšAureliaã®èªç©ºåçã®é·æãšçæ
ãã¬ãŒã ã¯ãŒã¯ãéžæãããšãã«æ¯èŒã§ããå ·äœçãªç¹æ§ãããããã©ãŒãã³ã¹ãèŠãŠã¿ãŸãããã Aureliaã¯dbmonsterãã³ãããŒã¯ã§èå³æ·±ãçµæã瀺ããŠããã Angular 2ãããããã«åªãããã€ã³ããå©ãã Reactããã³Angular1ãããé¡èã«åªããŠããŸãã
dbmonsterãšã¯
Dbmonsterã¯ã Ryan Florenceã«ãã£ãŠéçºãããã¬ã³ããªã³ã°ãã³ãããŒã¯ã§ãã äœæ¥ã®é床ãè©äŸ¡ããããã«ã絶ããæŽæ°ãããããŒã¿ãå«ãé
åãã¬ã³ããªã³ã°ãããŸããããã«ããããã¬ãŒã ã¯ãŒã¯ã®é床ãè©äŸ¡ã§ããŸãã ãã®ãã¹ãã¯å
ã
ãAngular 1ãAngular 2ãReactçšã«äœæãããŸããã Aureliaã®éçºè
ã®1人ã§ããJeremy Danyowã¯ãåŸã«Aureliaã®å®è£
ãå°å
¥ããŸãã ã
ãã³ãããŒã¯ãè©äŸ¡ãããšãã¯ã次ã®ç¹ã«æ³šæããããšã圹ç«ã¡ãŸãã
ãã¹ãŠã®ã€ã³ãžã±ãŒã¿ãŒãæ£ããæ©èœããæããã¯ãªãŒã³ãªãçµæãåŸãã«ã¯ãã¯ãã ãã©ãŠã¶ãŒã䜿çšãã次ã®ã³ãã³ãã§å®è¡ããããšããå§ãããŸãã
ãã³ãããŒã¯ãè©äŸ¡ãããšãã¯ã次ã®ç¹ã«æ³šæããããšã圹ç«ã¡ãŸãã
- ã¹ã ãŒãºãªã¹ã¯ããŒã«-ããŒãžã¯ããžã£ã³ããããã«ã¹ã¯ããŒã«ããå¿ èŠããããŸã
- ããŒã«ããã-ããŠã¹ã«ãŒãœã«ããªã¹ãäžã«ç§»åãããšãããŒã«ããããæç»ãããŸãã ã¹ã ãŒãºã«æç»ããããŒã¿ãé æ»ãªã衚瀺ããå¿ èŠããããŸãã
- åæç»é床ãšã¡ã¢ãªé床-å³äžé ã«2ã€ã®ã€ã³ãžã±ãŒã¿ããããŸãã æåã¯1ç§ãããã®åæç»æ°ã瀺ãã2çªç®ã¯ã¡ã¢ãªæ¶è²»éã瀺ããŸã
- ããŒã¿å€æŽã®é床-ããŒãžã®äžéšã«ã¯ãããŒã¿å€æŽã®é »åºŠã調æŽã§ããã¹ã©ã€ããŒããããŸãã ããŒã¿ã®å€æŽçãäœäžããŠãåæç»çãå¢å ããªãå Žåãããã¯ãåé¡ã®ãã¬ãŒã ã¯ãŒã¯ãå€æŽãéå¹ççã«ç£èŠããDOMã®æŽæ°ã«é¢ãã決å®ãäžãããšãæå³ããŸãã
ãã¹ãŠã®ã€ã³ãžã±ãŒã¿ãŒãæ£ããæ©èœããæããã¯ãªãŒã³ãªãçµæãåŸãã«ã¯ãã¯ãã ãã©ãŠã¶ãŒã䜿çšãã次ã®ã³ãã³ãã§å®è¡ããããšããå§ãããŸãã
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir="C:\chrome\dev-sessions\perf" --enable-precise-memory-info --enable-benchmarking --js-flags="--expose-gc"
ç¡åœ¢ã®ç¹æ§ãããç§ã¯äž¡æ¹ã®ãã¬ãŒã ã¯ãŒã¯ã®æ匷ãšæ匱ã®åŽé¢ãè©äŸ¡ããããšããŸãïŒèè ãç§èŠïŒã
Angularã®äž»ãªå©ç¹
- Angularã®ã³ãã¥ããã£ã¯æããã«å€§ãããããã¢ã€ãã¢ãæ¡åŒµæ©èœãå¢ããæ¢åã®è³ªåã«å¯ŸããçããèŠã€ãããããªããŸãã
- Angularã«ã¯3åã®ããŒã ãããããããžã§ã¯ãã®éçºãã¯ããã«é«éã§ãã ãŸããAngularããŒã ã¯æ¢ã«éçºè
ããŒã«ã®äœæè
ã®ãµããŒãã確ä¿ããŠãããAngularã®ãµããŒãã¯éââåžžã«å€ãã®ããŒã«ã§è¡ãããŸãïŒãŸããéçºè
ãã¬ãã¥ãŒã§ããæ¢ã«åäœããéåžžã«ã¯ãŒã«ãªBatarangleããããŸãïŒã
ã¢ãŠã¬ãªã¢ã®äž»ãªå©ç¹
- Aureliaã¯ãAngularãšå€ãã®èå³æ·±ãæ©èœã察æ¯ã§ããŸãã ããšãã°ãããã¯é«åºŠãªæ§æã¡ã«ããºã ã§ããã ãã³ãã¬ãŒãããŒãã§ãã Aureliaã¯ç®ç«ããªãããã«èšèšãããŠãããæçµçãªã³ãŒãã®ãã¬ãŒã ã¯ãŒã¯èšèšã®æ°ã¯æå°éã§ãã Aureliaã¯ããã³ã³ãã¯ãã§ç¶æãããŸãããAngularã¯ã³ããŒããŒã¹ãã匷å¶çã«çæããããšããããŸãã
- AureliaããŒã ã¯åçšã®é¡§å®¢ãµããŒããæäŸãããããžã§ã¯ãã®æ¹åæ§ã«åœ±é¿ãäžããæ¬åœã®æ©äŒããããŸãã ãŸããAngular 2ã®å ŽåãææããŠãããã®ã䜿çšããå¿ èŠããããŸãã
Angularã®äž»ãªæ¬ ç¹
- Angularã®éçºå±¥æŽãèŠããšãAngular 2ãæçµçã«ã©ããªãããããŒã ãã¯ã£ãããšèŠãŠããªããšããå°è±¡ãåããŸãã ç§ã¯Angular 2ã®äœæ¥ã®çºè¡šä»¥æ¥Angular 2ããã©ããŒããŠããŸãããããã¬ãŒã ã¯ãŒã¯ã¯æ¬åœã«éåžžã«åŒ·åã§ãããåžžã«é 次å€æŽãããããã§ã¯ãããŸããã åãçç±ã§ãã¢ã€ãŒã³ããŒã°ã¯ãããžã§ã¯ãã®ã¢ãŒããã¯ãã£ãå
ã®ã¢ãŒããã¯ãã£ããã©ã®ããã«å€ãã£ãã®ãã®ããã«Angular 2ããŒã ãå»ããŸããã åãçç±ã§ã åæ§ã®æçš¿ã衚瀺ãããŸãã
ã¢ãŠã¬ãªã¢ã®äž»ãªæ¬ ç¹
- ã¢ãŠã¬ãªã¢ã«é¢ããäž»ãªè³ªåã¯ã圌女ãAngular 2ã«å察ãããã©ããã§ããã¢ã€ãŒã³ããŒã°ã¯ã¢ãŠã¬ãªã¢ãšAngular 2ã察æ¯ããŠãããããããã¯æ questionãªè³ªåã§ã¯ãããŸãããããã«ã Angular 2ã®èšäºã«é¢ããã³ã¡ã³ãããããŸãã ç§ã¯ã¢ãŠã¬ãªã¢ãæ¬åœã«å¥œãã§ãããèè
ãç²ããŠãããããéžæãããã¬ãŒã ã¯ãŒã¯ããã€ããµããŒããããªããªãå¯èœæ§ãå¿é
ã§ãã äžæ¹ã倧ããªFacebook ãparse.comã®ééãçºè¡šããŠãã1é±éãçµã£ãŠããªãããã誰ãäœã®åœ±é¿ãåããŸããã
Angular 2ãšAureliaã®æ©èœãçšèªãä¿åãããã®
Angular 2ãšAureliaã®ã¢ãŒããã¯ãã£ã¯éåžžã«äŒŒãŠããŸãã 以äžã§ã¯ãäž¡æ¹ã®äœæ¥ã®ååãããã€ãã®æ®µèœã§å®åŒåããŠãæ€èšããã³æ¯èŒããã®ã«æå³ã®ããåºæ¬çãªçšèªãšæ§æãæäœã§ç€ºããŸãã ãããæ··ä¹±ããçšèªã§ã¯ãªããèªã¿ãããããã¹ãã«ãªãããšãé¡ã£ãŠããŸãã
Angular 2ããã³Aureliaã¢ããªã±ãŒã·ã§ã³ã®ã³ã¢ã¯ã察å¿ãããã³ãã¬ãŒãã«é¢é£ä»ããããã³ã³ããŒãã³ãã§ãã
ã¢ããªã±ãŒã·ã§ã³ïŒ app ïŒãè¡šãã«ãŒãã³ã³ããŒãã³ããå¿ èŠã§ã ã ãã³ã¬ãŒã¿ã䜿çšããŠãã¡ã¿ããŒã¿ãã³ã³ããŒãã³ãã«ãã€ã³ãããå¿ èŠããããŸãã
ã³ã³ããŒãã³ãã®åæåã¯ã äŸåæ§æ³šå ¥ã䜿çšããŠå®è¡ãããŸãã ãŸããåã³ã³ããŒãã³ãã«ã¯ã ã©ã€ããµã€ã¯ã«ããã¯ãšçµ±åã§ãã宣èšãããã©ã€ããµã€ã¯ã«ããããŸã ã ã³ã³ããŒãã³ãã¯éå±€æ§é ã«é 眮ã§ããŸãã
ã³ã³ããŒãã³ããšãã³ãã¬ãŒãéã®ç¶æ ã®åæãšéä¿¡ã¯ã ããŒã¿ãã€ã³ãã£ã³ã°ã䜿çšããŠå®è¡ãããŸã ã ãã€ã ïŒAngularïŒãŸãã¯å€ã³ã³ããŒã¿ãŒ+ãã€ã³ãã£ã³ã°åäœ ïŒAureliaïŒã䜿çšããŠããã³ãã¬ãŒããæçµçãªHTMLã«åã蟌ãããšãã§ããŸãã
ã«ãŒãã£ã³ã°ã¯ ãã¢ããªã±ãŒã·ã§ã³ã®éé¢ãããé åéã移åããããã«äœ¿çšãããŸãã ã€ãã³ãã¯ãã¢ããªã±ãŒã·ã§ã³ã¢ãžã¥ãŒã«éã®éä¿¡ã«äœ¿çšã§ããŸãã
ãããŠæåŸã«ãäŸã«ç§»ããŸãã
æåã®ã³ã³ããŒãã³ããäœæãã
è§åºŠ2
ãŸããã«ãŒãã³ã³ããŒãã³ããšãªãæãåçŽãªã³ã³ããŒãã³ããäœæããããšããå§ããŸãããã
import {Component} from 'angular2/core'; @Component({selector: 'angular-app', templateUrl: 'app/app.html'} }) export class App { message: string = 'Welcome to Angular 2!'; }
Angularãã¢ãžã¥ãŒã«ãã³ã³ããŒãã³ãã§ããããšãç解ããã«ã¯ã@ Componentãã³ã¬ãŒã¿ãŒã§ã¢ãžã¥ãŒã«ãã©ããããå¿ èŠããããŸãã
泚-å³å¯ã«èšãã°ã@ Componentã¯ãã³ã¬ãŒã¿ãŒã§ã¯ãªãã¢ãããŒã·ã§ã³ã§ãã ããã§éãã«ã€ããŠèªãããšãã§ããŸã ã èšäºã§ã¯ãAngular 2ã¢ãããŒã·ã§ã³ã®ããã¥ã¡ã³ãã§ã¯ããã³ã¬ãŒã¿ãã»ã¯ã·ã§ã³ã«ããããããã³ã¬ãŒã¿ãšããçšèªã¯æ®ããŠãããŸãã
å°ãªããšãããã³ã¬ãŒã¿ã¯ã»ã¬ã¯ã¿ããã³ãã¬ãŒããæç»ããå Žæã瀺ãå¿ èŠããããŸãã ãã®å Žåãããã¯<angular-app>èŠçŽ ã§ãã
ãã³ãã¬ãŒãã宣èšããããã®2ã€ã®ãªãã·ã§ã³ããããŸãã
- @Componentãã³ã¬ãŒã¿ã®ãã³ãã¬ãŒããã©ã¡ãŒã¿ãšããŠHTMLæååãæå®ããŸãã ãã®ã¢ãããŒãã¯ããã³ãã¬ãŒããã³ã³ãã¯ãã§ãå¥ã®HTMLããŒãžãäœæããããªãå Žåã«åœ¹ç«ã¡ãŸãã
- ãã³ãã¬ãŒãã®URLãtemplateUrlãã©ã¡ãŒã¿ãŒãšããŠæå®ããŸãã å¥ã®ããŒãžãå¿ èŠãªã®ã§ããã®ãªãã·ã§ã³ã䜿çšããŸã
ãã³ãã¬ãŒãã¯æ¬¡ã®ããã«ãªããŸãã
<div class="container"> <div class="navbar-header"> <a class="navbar-brand" href="#">{{message}}</a> </div> </div>
å®éãããã¯ã³ã³ããŒãã³ãå šäœã§ãã ç°¡åã§ãã
è¯ãããš -ã³ãŒãã¯ãAngular 1ããããã¬ãŒã ã¯ãŒã¯èªäœã®æ§é ããããã¯ãªãŒã³ã§èªç±ã§ããããã¯æå ±ã§ãã
æãåçŽãªã³ã³ããŒãã³ãã§ãã£ãŠããã³ã¬ãŒã¿ã䜿çšããå¿ èŠãããã®ã¯é¢åã§ã ã ããã¥ã¡ã³ãããåŒçšïŒ
åAngularã³ã³ããŒãã³ãã«ã¯ãåäžã®@Componentãšå°ãªããšã1ã€ã®Viewã¢ãããŒã·ã§ã³ãå¿ èŠã§ãã @Componentã¢ãããŒã·ã§ã³ã¯ãã³ã³ããŒãã³ããã€ã³ã¹ã¿ã³ã¹åãããã¿ã€ãã³ã°ãããã³ã³ã³ããŒãã³ãããã€ã³ãããããããã£ãšhostListenerãæå®ããŸãã
ãã®å Žåããã®äŸã§æ§æããããã©ã¡ãŒã¿ãŒã¯ãã»ãšãã©ã®å Žåãããçš®ã®ããžãã¯ã«åŸããŸããæ瀺çãªæ§æã®ä»£ããã«ãèŠåã䜿çšã§ããŸããããè€éãªã·ããªãªã§ã¯ãã³ã¬ãŒã¿ãŒãæ¥ç¶ããå¿ èŠããããŸãã
ã¢ãŠã¬ãªã¢
Aureliaã¯èŠçŽã«åºã¥ããŠæ§ç¯ãããŠãããããã³ã³ããŒãã³ãã¯ã¡ã¿ããŒã¿ã®ãªãéåžžã®ã¢ãžã¥ãŒã«ã§ãã
export class App { message: string = "Welcome to Aurelia!"; }
æšæºã®èŠåã«åŸã£ãŠããã³ãã¬ãŒãã®å®£èšã®ããã«ãã³ã³ããŒãã³ãã«é¡äŒŒããååã®htmlãã¡ã€ã«ãäœæããå¿ èŠããããŸããã€ãŸãããã®äŸã§ã¯app.htmlãã¡ã€ã«ã«ãªããŸãã
<template> <div class="container"> <div class="navbar-header"> <a class="navbar-brand" href="#">${message}</a> </div> </div> </template>
Aureliaã®åãã³ãã¬ãŒãã¯ã ãã³ãã¬ãŒãèŠçŽ ã§ã©ããããå¿ èŠããããŸãã Angular 2ãšåæ§ã®ã€ã³ã©ã€ã³ãã³ãã¬ãŒããäœæããã«ã¯ã inlineViewãã³ã¬ãŒã¿ã䜿çšã§ããŸãã ãŸããAureliaã§ã¯ãèŠåãå€æŽããé«åºŠãªã«ã¹ã¿ãã€ãºãå®è¡ã§ããŸãã 詳现ã¯ãã¡ããã芧ãã ãã ã
è¯ãããš -ã³ãŒãã¯éåžžã«ã¯ãªãŒã³ã§ç解ãããããã®ã§ãã ãã¬ãŒã ã¯ãŒã¯ã®èšèšã¯ãããŸããã
äœãæ°ã«ãªã -ããªãã¯åæ§ã®åé¡ã解決ããããã«å€ãã®æ³šéãèšå®ããå¿ èŠããããããããŸããã ããšãã°ããããã¯inlineViewãnoViewãuseViewãuseViewStrategyã§ãã ããã¥ã¡ã³ãã¯ãŸã ååã§ã¯ãªããæ€çŽ¢ããããŠããªããããäœãã©ãã§äœ¿çšãããã«ã€ããŠåçŽã«æ··ä¹±ãããªã¹ã¯ããããŸãã
ã«ãŒãã£ã³ã°ãæ§æããŸã
ç§ãã¡ã®å Žåãåã¢ããªã±ãŒã·ã§ã³ã«ã¯ããã€ãã®ããŒãžããããèšäºã§æŠèª¬ãããŠããåŽé¢ãèæ ®ãããŸãã AngularãšAureliaã®äž¡æ¹ã§ãã«ãŒãã£ã³ã°èªäœãšæ¬è³ªçã«åãã§ããããããã¹ããããã«ãŒãã£ã³ã°ã¯èæ ®ããŸããã
è§åºŠ2
Angular 2ã§ã«ãŒãã£ã³ã°ãèšå®ããã«ã¯ã@ RouteConfigãã³ã¬ãŒã¿ãã€ã³ããŒãããã«ãŒãã«ãã€ã³ããããã¢ãžã¥ãŒã«ãã€ã³ããŒãããŠãã«ãŒãã£ã³ã°ãããã宣èšããå¿ èŠããããŸãã ã¢ããªã³ã³ããŒãã³ãã§ãããè¡ããŸãã
... import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router'; import {BindingSample} from './binding-sample/binding-sample'; import {ComponentSample} from './component-sample/component-sample'; ... @RouteConfig([ { path: '/component-sample', name: 'ComponentSample', component: ComponentSample, useAsDefault: true }, { path: '/binding-sample', name: 'BindingSample', component: BindingSample } ]) export class App { ... }
åã«ãŒãã«ã€ããŠã以äžã瀺ããŸãã
- ã«ãŒããã¿ãŒã³
- ã«ãŒãåïŒããŒã¯ã¢ããã§ã®ãã€ã³ãã«å¿ èŠïŒ
- ã«ãŒããã¢ã¯ãã£ãåããããšãã«Angularãäœæããã³ã³ããŒãã³ãã¢ãžã¥ãŒã«
- ãããããã©ã«ãã«ãŒãã§ããããšã瀺ãuseAsDefaultãªãã·ã§ã³ãã©ã¡ãŒã¿
éå§æã«ãã§ã«ã«ãŒãããããã¹ãŠã®ã¢ãžã¥ãŒã«ãããŒãããå¿ èŠãããããšã«æ··ä¹±ããŠããå Žåã¯ãéåæããŒãã䜿çšã§ããŸãã ãããè¡ãã«ã¯ãã³ã³ããŒãã³ããã©ã¡ãŒã¿ãŒã®ä»£ããã«ãããŒããŒãã©ã¡ãŒã¿ãŒã䜿çšããŠãç®çã®ã¢ãžã¥ãŒã«ãã€ã³ããŒããããããã¹ãè¿ãé¢æ°ãæå®ããŸãã ããããŸãããã³ãŒãäŸã¯å°ãåŸã§ãã
ãã³ãã¬ãŒãã§ã«ãŒãã£ã³ã°ã䜿çšããŠããã²ãŒã·ã§ã³ãæç»ããçŸåšã®ã³ã³ããŒãã³ããæç»ããã»ã¯ã·ã§ã³ã瀺ãã«ã¯ãROUTER_DIRECTIVESãã£ã¬ã¯ãã£ãã®ã³ã¬ã¯ã·ã§ã³ãè¿œå ã€ã³ããŒããã@ Componentãã³ã¬ãŒã¿ãŒã®ãã£ã¬ã¯ãã£ããã©ã¡ãŒã¿ãŒã«è¿œå ããå¿ èŠããããŸãã åèšã§ãapp.tsã¢ãžã¥ãŒã«ã¯æ¬¡ã®ããã«ãªããŸãã
import {Component} from 'angular2/core'; import {View} from 'angular2/core'; import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router'; @Component({ selector: 'angular-app', templateUrl: 'app/app.html', directives: [ROUTER_DIRECTIVES] }) @RouteConfig([ { path: '/component-sample', name: 'ComponentSample', loader : () => System.import('app/component-sample/component-sample').then(m => m.ComponentSample), useAsDefault: true }, { path: '/binding-sample', name: 'BindingSample', loader : () => System.import('app/binding-sample/binding-sample').then(m => m.BindingSample) } ]) export class App { message: string = "Welcome to Angular 2!"; }
次ã«ãapp.htmlã«ããã²ãŒã·ã§ã³ãè¿œå ããŸãã ãããè¡ãã«ã¯ã routerLinkãã£ã¬ã¯ãã£ãã䜿çšãããã©ã¡ãŒã¿ãŒãšããŠé åãæž¡ããŸãããã®æåã®èŠçŽ ã¯ã@ RouteConfigã®ã»ããã¢ããæã«æå®ããã«ãŒãåãå«ãæååã§ãã
⊠<div id="navbar" class="collapse navbar-collapse"> <ul class="nav navbar-nav"> <li class="active"> <a [routerLink]="['ComponentSample']">Component sample</a> </li> <li> <a [routerLink]="['BindingSample']">Binding sample</a> </li> </ul> </div> ...
ãã¹ããããã«ãŒãã£ã³ã°ã®å Žåããã©ã¡ãŒã¿ãŒã¯é åã§ãããã®åŸãã«ãŒãã®ååãå«ãæ°è¡ã®é åãæž¡ããŸãã
ã«ãŒãããã©ã¡ãŒã¿ãŒåããå¿ èŠãããå Žåããã©ã¡ãŒã¿ãŒãæã€ãªããžã§ã¯ãã¯é åã®æåŸã®èŠçŽ ã«ãã£ãŠéä¿¡ãããŸãã
... <a [routerLink]="['BindingSample', {someParameter: 'someString'}]">Binding sample</a> ...
ãããŠæåŸã®ç¬éã çŸåšã®ã«ãŒãã®ãã³ãã¬ãŒããæç»ãããé åãããŒã¯ã¢ããã§ç€ºãå¿ èŠããããŸãã åãapp.htmlã§router-outletãã£ã¬ã¯ãã£ãã䜿çšããŠãããè¡ããŸãã
<div class="container"> <router-outlet></router-outlet> </div>
è¯ãããš -æ°ããã«ãŒãã£ã³ã°ã¯ãAngular 1.xã«ååšããŠããrouteProviderãããåªããŠããŸãããããã§ãå€ãã®çåãçããŸãã ããã¥ã¡ã³ãå ã®ã«ãŒãã£ã³ã°ã«é¢é£ããã»ãšãã©ã®ã¿ã€ãã«ã¯ãŸã 説æããŸã£ãããªããããæ確ã«èª¬æããããšã¯å°é£ã§ãã
æ··ä¹±ããããã® -ããã€ãã®ããšãæ··ä¹±ãããŸãã ãŸã第äžã«ãããã¯ãã³ã¬ãŒã¿ãä»ããŠèšå®ããå¿ èŠããããŸã-ããšãã°ãã¢ããªã±ãŒã·ã§ã³ã«50ã®ã«ãŒããããå Žåãã¢ãžã¥ãŒã«ã®ã³ãŒãã¯ããããã¹ãŠã®èšå®ã§åçŽã«å€±ãããŸãã ãŸããããã²ãŒã·ã§ã³ã¹ããŒã ãæ§ç¯ãããšãã«if-logicãå¿ èŠãªå Žåãã³ãŒããæªå€¢ã«ãªããªã¹ã¯ããããŸãã
2ã€ç®ã¯ãã«ãŒãã®ã³ã¬ã¯ã·ã§ã³å šäœãžã®æ瀺çãªã¢ã¯ã»ã¹ããªãããšã§ããããã¯ãåãªã³ã¯ãæåã§ã¬ã³ããªã³ã°ããã®ã§ã¯ãªããããŒã¯ã¢ããã§ç°¡åã«ãœãŒãããŠããã²ãŒã·ã§ã³å šäœãã¬ã³ããªã³ã°ã§ããŸãïŒå¿ããŸãïŒã ç¹°ãè¿ããŸãããã«ãŒããæ§ç¯ããããã®if-logicãããå Žåããã³ãã¬ãŒãã«ãã®ããžãã¯ãè€è£œããŠãããŸãå€ãæç»ããªãããã«ããå¿ èŠããããŸãã
ã¢ãŠã¬ãªã¢
Aureliaã®èŠçŽã«åŸã£ãŠãã³ã³ããŒãã³ãã®ã«ãŒãã£ã³ã°ãæ§æããã«ã¯ãconfigureRouterã¡ãœãããå®è£ ããå¿ èŠããããŸãããã®ã¡ãœããã¯Aureliaã«ãã£ãŠèªåçã«åŒã³åºãããŸãã ãã¹ããããã«ãŒãã£ã³ã°ã«ãåãããšãèšããŸããconfigureRouterã¡ãœãããæã€ã³ã³ããŒãã³ãã¯ãã«ãŒãã£ã³ã°ã¹ããŒã ã圢æããŸãã
export class App { message: string = "Welcome to Aurelia!"; router: any; configureRouter(config, router) { config.title = 'Welcome to Aurelia!'; config.map([ { route: ['', 'component-sample'], moduleId: 'app/component-sample/component-sample', nav: true, title: 'Component sample' }, { route: 'component-sample', moduleId: 'app/binding-sample/binding-sample', nav: true, title: 'Binding sample' } ]); this.router = router; } }
åã«ãŒãã«ã€ããŠã以äžã瀺ããŸãã
- ã«ãŒããã¿ãŒã³ïŒãŸãã¯äžé£ã®ãã¿ãŒã³ã空çœè¡ãã¿ãŒã³ã¯ããã©ã«ãã«ãŒããæå³ããŸãïŒã
- ã«ãŒããã¢ã¯ãã£ãã«ãããšãã«éå§ããã¢ãžã¥ãŒã«ã®èå¥åã
- ãªãã·ã§ã³ã®ã¿ã€ãã«ããããã£-ã«ãŒããã¢ã¯ãã£ãã«ãããšããã®å€ãããŒãžã¿ã€ãã«ã«è¿œå ãããŸãã
- ãªãã·ã§ã³ã®navããããã£-ã«ãŒããããã²ãŒã·ã§ã³ã¢ãã«ã«å«ãããã©ãããæå®ããŸãã æ°å€ãæå®ããå Žåãããã²ãŒã·ã§ã³ã¢ãã«ã³ã¬ã¯ã·ã§ã³å ã®èŠçŽ ã®é åºãæå³ããŸãã
転éãããæ§æã«åºã¥ããŠãAureliaã¯ããã²ãŒã·ã§ã³ã¢ãã«ãäœæããŸããããã¯ãã³ãã¬ãŒãå ã§ãœãŒããããããã²ãŒã·ã§ã³ãã¬ã³ããªã³ã°ã§ããŸãã
<div id="navbar" class="collapse navbar-collapse"> <ul class="nav navbar-nav"> <li repeat.for="row of router.navigation" class="${row.isActive ? 'active' : ''}"> <a href.bind="row.href">${row.title}</a> </li> </ul> </div>
æåŸã®ã¹ãããã¯ãçŸåšã®ã«ãŒãã®ãã³ãã¬ãŒããæç»ããããšãªã¢ãããŒã¯ã¢ããã§ç€ºãããšã§ãã åãapp.htmlã§router-viewãã£ã¬ã¯ãã£ãã䜿çšããŠãããè¡ããŸã
<div class="container"> <router-view></router-view> </div>
è¯ãããš -ã»ããã¢ããã¯ç°¡åã§ãéåžžã«æçœã§ãã Aureliaãç§ãã¡ãå©ããããšããããã²ãŒã·ã§ã³çšã®ã³ã¬ã¯ã·ã§ã³ãæ§ç¯ããŠããã®ã¯çŽ æŽãããããšã§ãã
çŽããããããš -ããã²ãŒã·ã§ã³ãšæ¯èŒããŠãDurandalã¯ã«ãŒãèšå®ã«ä»»æã®ããããã£ãè¿œå ããæ©èœãåé€ããŸããã äžæ¹ã§ãããã¯ãnefigã§ããããããã¡ããæ£ããã§ãã äžæ¹ãããã«ãããæåã¬ã³ããªã³ã°ã®æ¹åã§ããã²ãŒã·ã§ã³ã¢ãã«ã䜿çšããå¯èœæ§ãå€§å¹ ã«æžå°ããŸãã ã¡ãã¥ãŒé ç®ã«ã¿ã€ãã«ã ãã§ãªããããšãã°ããŒã«ããããè¿œå ããå Žåãããã²ãŒã·ã§ã³ã¢ãã«ã¯åœ¹ã«ç«ã¡ãŸããã
ãã¹ããããã³ã³ããŒãã³ããè¿œå ãã
è§åºŠ2
ãã¹ããããžã§ã¯ãã§ãã¹ããããã³ã³ããŒãã³ããæäœããäŸã«ã€ããŠã¯ãã³ã³ããŒãã³ããµã³ãã«ãã©ã«ããŒããããå¿ èŠãªãã®ããã¹ãŠå«ãŸããŠããŸãã
ãããã£ãŠãAngular 2ã§ãã¹ããããã³ã³ããŒãã³ããäœæããã«ã¯ã次ãå¿ èŠã§ãã
- ã³ã³ããŒãã³ãã宣èšããã¡ã¿ããŒã¿ããã€ã³ãããŸãã 芪ã³ã³ããŒãã³ãã®ããŒã¿ã䜿çšããŠããããã£ãåæåããã«ã¯ã@ Componentãã³ã¬ãŒã¿ã«å
¥åãã©ã¡ãŒã¿ãè¿œå ãã察å¿ããããããã£ã®ååãæž¡ããŸãã
import {Component} from 'angular2/core'; @Component({ selector: 'test-child-component', inputs: ['inputMessage'], template: `<div class="panel panel-default"> <div class="panel-heading">Child component title</div> <div class="panel-body"> Message from parent component is: {{inputMessage}} </div> </div>` }) export class TestChildComponent { inputMessage: string }
- 芪ã³ã³ããŒãã³ãïŒcomponent-sample.tsïŒã«åã³ã³ããŒãã³ããã€ã³ããŒããã@ Componentãã³ã¬ãŒã¿ãŒã®ãã£ã¬ã¯ãã£ãé åã«æž¡ããŸã
- 芪ã³ã³ããŒãã³ãã®ãã³ãã¬ãŒãã«ããã¹ããããã³ã³ããŒãã³ãããæå®ãããã»ã¬ã¯ã¿ãŒã«å¯Ÿå¿ããèŠçŽ ãé 眮ãããã©ã¡ãŒã¿ãŒãæž¡ããŸã
ãã®çµæã芪ã³ã³ããŒãã³ãã¯æ¬¡ã®ããã«ãªããŸãã
import {Component} from 'angular2/core'; import {TestChildComponent} from './test-hild-omponent'; @Component({ template: ` <div class="sample-header"> <h1>{{message}}</h1> </div> <test-child-component [inputMessage]="messageForChild"></test-child-component>`, directives: [TestChildComponent] }) export class ComponentSample { message: string = 'This is a component with child component sample'; messageForChild: string = 'Hello to child component!'; }
è¯ãããš -äžè¬çã«ããã¹ãŠãã·ã³ãã«ã§æ確ã§ãã
ããªããæ©ãã®ã¯ãã¢ãŠã¬ãªã¢ãæã£ãŠããçŽ æµãªãã³ããªãããšã§ãã 以äžã«èª¬æããŸãã
ã¢ãŠã¬ãªã¢
Aureliaã§ã¯ãã«ã¹ã¿ã èŠçŽ ãšæ§æãšãã2ã€ã®æ¹æ³ã§ãã¹ããããã³ã³ããŒãã³ããæç»ã§ããŸãã
äžè¬ã«ãæåã®æ¹æ³ã¯Angular 2ã«äŒŒãŠãããè€éãªã³ã³ãããŒã«ã®äœæãªã©ã«äœ¿çšãããŸãã ãã¹ããããžã§ã¯ãã³ãŒãã§ã¯ããã®ã¢ãããŒãã¯test-custom-element.htmlãã¡ã€ã«ã§ç€ºãããŠããŸãã
2çªç®ã¯äž»ã«ãã¹ã¿ãŒè©³çŽ°ã¹ã¯ãªããã«äœ¿çšãããããæè»ã§ããããã¯ãããŒãããã³ã³ããŒãã³ããæç»ãããã³ãã¬ãŒãã転éããããŒã¿ãåçã«ç€ºãããšãã§ããããã§ãã ãã®ã¢ãããŒãã¯ããã¡ã€ã«test-hild-component.tsã®ãã¹ããããžã§ã¯ãã§å®èšŒãããŠããŸãã
äž¡æ¹ã®ãªãã·ã§ã³ãé çªã«åæããŸãã
ãªãã·ã§ã³1-ã«ã¹ã¿ã èŠçŽ ïŒ
ã«ã¹ã¿ã èŠçŽ ã䜿çšããŠãã¹ããããã³ã³ããŒãã³ããäœæããã«ã¯ã次ã®ãã®ãå¿ èŠã§ãã
- éåžžã®ã³ã³ããŒãã³ããäœæããããã«ãã³ã¬ãŒã¿ããããã£ã§@bindableãããŒã¯ããŸãããã©ã¡ãŒã¿ãšããŠãã³ãã¬ãŒããééããå€ïŒAngular 2ã®å
¥åãã©ã¡ãŒã¿ã«äŒŒãŠããŸãïŒã ããã«ãèŠçŽ ãåçŽã§åäœããªãå Žåã¯ãã³ã³ããŒãã³ããäœæããã«ããã³ãã¬ãŒããäœæããåãååã®å±æ§ã䜿çšããŠãã€ã³ãå¯èœãªããããã£ãäžèŠ§è¡šç€ºããã ãã§æžã¿ãŸãã Aureliaã³ã³ããŒãã³ãã¯ããªã³ã¶ãã©ã€ãã§äœæããŸãïŒ
<template bindable="message"> <div class="panel panel-default"> <div class="panel-heading">Custom element title</div> <div class="panel-body"> Message from parent component is: ${message} </div> </div> </template>
- å¿
èŠãªã¢ãžã¥ãŒã«ãæã芪ã³ã³ããŒãã³ãã®ãã³ãã¬ãŒãã«requireèŠçŽ ãè¿œå ãïŒAngular 2ã®ãã£ã¬ã¯ãã£ããã©ã¡ãŒã¿ãŒã«äŒŒãŠããŸããããã³ãã¬ãŒãå
ïŒãå¿
èŠãªå Žæã«ããŒã¯ã¢ããã§ã«ã¹ã¿ã èŠçŽ ãæç»ããŸãã ãã©ã¡ãŒã¿ãŒã¯ã<property name> .bindå±æ§ã䜿çšããŠæž¡ãããŸãã ãã®çµæã芪ã³ã³ããŒãã³ãã®ãã³ãã¬ãŒãã¯æ¬¡ã®ããã«ãªããŸãã
<template> ... <require from="app/component-sample/test-custom-element.html"></require> <test-custom-element message.bind="messageForCustomElement"></test-custom-element> </template>
èŠçŽ ãæ®éçã«äœ¿çšãããå Žåãã°ããŒãã«ãªãœãŒã¹ãšããŠç»é²ã§ããŸãã ããã«ãããåãã³ãã¬ãŒãã«<require ...>ãèšè¿°ã§ããªããªããŸãã ãã®æ¹æ³ã«ã€ããŠã¯ ãããªãœãŒã¹ã®ã°ããŒãã«åãã»ã¯ã·ã§ã³ã«èšèŒãããŠããŸãã
Aureliaã«ã¯ãAngularã§èŠã€ããããšãã§ããªãã£ããã1ã€ã®åªãããªãã·ã§ã³ãšããŠã芪ãã³ãã¬ãŒãããããŒã¯ã¢ãããæž¡ãæ¹æ³ããããŸãã èŠçŽ test-custom-elementå ã®èŠªãã³ãã¬ãŒãã§ããŒã¯ã¢ããã宣èšããåã§èŠçŽ ãè¿œå ããå Žå
<content></content>
次ã«ã芪ãã³ãã¬ãŒãããã®ããŒã¯ã¢ãããåã«æç»ãããŸãã ãŸããã«ã¹ã¿ã èŠçŽ ã䜿çšããå Žåãåè¿°ã®ãã³ãã¬ãŒãããŒãã䜿çšããŠãããã€ãã®äº€æå¯èœãªé åã宣èšã§ããŸãã
è¯ãããš -ãã¹ãŠãã·ã³ãã«ã§è«ççã§ãã
æ°ã«ãªãããš -誰ããããŒã¯ã¢ããã§äŸåé¢ä¿ã宣èšããå¿ èŠããããšã¯éããŸããã
ãªãã·ã§ã³2-æ§æïŒ
ã³ã³ããžã·ã§ã³ã䜿çšããŠãã¹ããããã³ã³ããŒãã³ããäœæããã«ã¯ã次ã®ãã®ãå¿ èŠã§ãã
- éåžžã®ã³ã³ããŒãã³ããäœæããŸãã composeã¯åŒ±ãæ¥ç¶ãããã³ã³ããŒãã³ããæ³å®ããŠãããããããŒã¿è»¢éã«ã©ã€ããµã€ã¯ã«ããã¯ã䜿çšããå¿
èŠããããŸã ïŒAngular 2ã«ããããããããŸããã次ã®èšäºã§ã¯ãããã«ã€ããŠïŒã ãã®å Žåãactivateã¡ãœããã䜿çšããŸãã ãã³ãã¬ãŒãã¯å°ããã®ã§ãã€ã³ã©ã€ã³ã«ããŸãã
import {inlineView} from 'aurelia-templating'; @inlineView('<template><h3>${inputMessage}</h3><template>') export class TestChildComponent { inputMessage: string; activate(inputMessage: string) { this.inputMessage = inputMessage; } }
- 芪èŠçŽ ã®ãã³ãã¬ãŒãã«ãcomposeèŠçŽ ãé
眮ããŸãããã®èŠçŽ ã«ã¯ãåã³ã³ããŒãã³ããæã€ã¢ãžã¥ãŒã«ã®èå¥åã瀺ããŸãã 芪ã³ã³ããŒãã³ãããåã«ããŒã¿ã転éããã«ã¯ãmodel.bindå±æ§ã䜿çšããŸãã
<compose model.bind="messageForChild" view-model="app/testChildComponent"></compose>
ããã¯è¯ãããšã§ã -ç§ã®æèŠã§ã¯ãcomposeã¯ãããžã§ã¯ãã®è€éãã«å¯ŸåŠããããã®éåžžã«ã¯ãŒã«ãªæ¹æ³ã§ãã æè»æ§ãé«ããªãããã³ã³ããŒãã³ãéã®åé¢ãæäŸããŸãã
äœãæ°ã«ãªã -ç§ã¯é·ãéãã®é ç®ã«ã€ããŠèããŸããããç§ã¯äœãæžãããšãã§ããŸããã§ããã ç§ã¯æå¥ãèšãããšã¯äœããããŸããã
ããŒã¿ãã€ã³ãã£ã³ã°
è§åºŠ2
AngularããŒã ãæ°ããããŒã¿ãã€ã³ãã£ã³ã°ã®éçºã§è¡ã£ãäž»ãªäœæ¥ã¯ã䜿çšãããã£ã¬ã¯ãã£ãã®æ°ãæå°éã«æããããŒã¿ç§»åã®æ¹åãæ確ã«åºå¥ããããšã§ãããDOMãžãDOMãããåæ¹åã é çªã«ãã¹ãŠãèŠãŠã¿ãŸãããã
DOMãž
DOMãžã®åæ¹åãã€ã³ãã£ã³ã°ã¯ã2ã€ã®åœ¢åŒã䜿çšããŠå®è¡ãããŸãã
æååè£éïŒ
<div class="panel-body"> This string is builted with {{interpolationString}} syntax </div>
ããã³ããããã£ã®ãã€ã³ãïŒ
<img [src]="iconUrl" />
æååè£éã®å Žåãäºéå±±æ¬åŒ§ã§å²ãŸããããã¹ãã¯åŒãšããŠè©äŸ¡ãããå®è¡ããããã®çµæããã³ãã¬ãŒãã«é 眮ãããŸãã ã€ãŸããã³ã³ããŒãã³ãã®ããããã£ã ãã§ãªãããã®çš®ã®åŒããã€ã³ãããããšãã§ããŸãã
<h1>2 = {{1+1}}</h1>
ãã ãããã®æ©èœãä¹±çšãããè€éãªåŒãã³ã³ããŒãã³ãã®ããããã£ã«å ¥ããªãããšããå§ãããŸãã
ããããã£ãã€ã³ãã£ã³ã°ã®å ŽåãããŒã¯è§ãã£ãã§ããããã¯ããã³ãã¬ãŒããšã³ãžã³ã«ããããåŒã®çµæãå³ã«å²ãåœãŠãããããã£ã®ååã§ããããšãéç¥ããŸãããã®ãã©ãŒã ã§ã¯åŒããµããŒããããŠããŸãããä¹±çšã¯ãå§ãããŸãããæšæºã®ããããã£ã«å ããŠãrouterLinkãtextContentãªã©ã®è¿œå ã®ãã£ã¬ã¯ãã£ãã»ããããããŸãã
泚-Angular 2ã§ãã³ãã¬ãŒããéçºããå Žåãå®éã«ã¯HTMLã§ã¯ãªããç¹å¥ãªAngular 2æ§æã§èšè¿°ããŸããã³ãã¬ãŒãã解æããã«ã¯ãç¬èªã®ããŒãµãŒã䜿çšããŸãããããŠãããªããæžããšããäŸãã°ïŒ
<input value="some text" />
ããã¯ãå±æ§ã«å€ãå²ãåœãŠããããçŽç²ãªãHTMLã§ã¯ãªããå®æ°ã®å²ãåœãŠã«å解ãããæ§æç³è¡£ã§ãã
<input [value]="'some text'" />
éèŠãªç¹ã¯ãããŒã¿ãå±æ§ãã¯ã©ã¹åãããã³ã¹ã¿ã€ã«ã«ãã€ã³ãããããã«ãã¯ã©ã¹ãã¹ã¿ã€ã«ãããã³attrãã¬ãã£ãã¯ã¹ã䜿çšããå¥åã®æ§æãå¿ èŠã§ããããšã§ãã ããšãã°ã次ã®ããã«ïŒ
<img [style.width]="iconWidth" [style.height]="iconHeight" [src]="iconUrl" />
ã¹ã¿ã€ã«ã®è€æ°ã®ããããã£ãŸãã¯è€æ°ã®ã¯ã©ã¹ãåæã«èšå®ããããã®ngClassããã³ngStyleãã£ã¬ã¯ãã£ãããããŸããåŒã®çµæãšããŠãåããããã£ãã¹ã¿ã€ã«ããããã£ãŸãã¯ã¯ã©ã¹åãšããŠè©äŸ¡ããããªããžã§ã¯ããæåŸ ããŸãã
ããããã£ãã€ã³ãã£ã³ã°ãå±æ§ãã€ã³ãã£ã³ã°ããåé¢ãããã€ã³ãã¯ãhtmlå±æ§ãšDOMèŠçŽ ã®ããããã£ã«æ確ãªéããããããšã§ããå±æ§ã¯èŠçŽ ãåæåããããã«äœ¿çšãããããããã£ã¯èŠçŽ ã®ç¶æ ãå€æŽããããã«äœ¿çšãããŸããå±æ§ã䜿çšããã®ã¯Angular 2ã§ãã
ãã ãã
ãã®å Žåããã¹ãŠãã·ã³ãã«ã§ããããã¯ã€ãã³ããã³ãã©ãŒã®ãã€ã³ãã§ãã
<input type="button" (click)="onClicked()" value="Click me!" />
ãã®ããŒãžã§ã³ã®ããŒã¯æ¬åŒ§ã§ããã®äžã«ã€ãã³ãã®ååãæžããŸãïŒãonããã¬ãã£ãã¯ã¹ãªãïŒããã€ã³ãã£ã³ã°ã®å³åŽã®åŒã®äžã§ã¯ã$ã€ãã³ãå€æ°ã䜿çšå¯èœã§ããããã¯å®éã«ã¯DOMã€ãã³ãã§ããããã«ãããæãåçŽãªãã³ãã©ãŒã«å¯ŸããŠåã ã®é¢æ°ã宣èšãããã€ã³ã©ã€ã³åŒãèšè¿°ã§ããŸããããã ããç¹°ãè¿ããŸããããã®æ¹æ³ã§è€éãªæ§é ã«é¢äžããŠèšè¿°ããŠãæå³ããããŸããã
äž¡æ¹å
å®éãåæ¹åã®ããŒã¿ãã€ã³ãã£ã³ã°ã¯ãDOMããDOMãžã®çµã¿åããã䜿çšããŠå®è¡ã§ããŸãã
<input [value]="twoWayBindedProperty" (input)="twoWayBindedProperty=$event.target.value" />
ãã ããããã¯éåžžã«å žåçãªã·ããªãªã§ãããããAngularããŒã ã¯ç¹å¥ãªngModelãã£ã¬ã¯ãã£ããéçºããŸããã
<input [(ngModel)]="twoWayBindedProperty" />
å¿ èŠã«å¿ããŠãå šäœãåå¥ã®éšåã«å解ããDOMããã®æ瀺ã®ããã«ngModelChangeãã£ã¬ã¯ãã£ããå¿ èŠã§ãã
<input [ngModel]="twoWayBindedProperty" (ngModelChange)="twoWayBindedProperty=$event">
泚ïŒ[]ãïŒïŒb [ïŒïŒ]ã®æ§æãšæ··åãããŠãããæšæºãªãã·ã§ã³ã奜ãå Žåãæ¥é èŸbind-ãon-ãbindon-ããããã䜿çšã§ããŸããäŸïŒ
<img bind-src="iconUrl">
ããã¯è¯ãããšã§ã-æ§ããã«èšã£ãŠããæ§æãç°åžžã§ããã圌ã«åããããå€ãã®åŠå®çãªå£°æãæ¢ã«ãããšããäºå®ã«ãããããããç§ã¯åœŒãå人çã«å¥œãã§ãããŸããããã¯éåžžã«é¡èã§ãã第äºã«ãèŠããããããšã§ãã第äžã«ã圌ã¯ããŸããŸãªç·šéè ã®ã€ã³ããªã»ã³ã¹ãšè¯ãåéã«ãªããŸããããã¯æåã®ãã£ã©ã¯ã¿ãŒããããããAngular 2ã³ã³ã¹ãã©ã¯ãã§ããããšã¯æããã§ãããããã«ãã£ã¬ã¯ãã£ããªãã·ã§ã³ãåãåãããããã®ngClassãngModelãªã©ããã¹ãŠèŠããå¿ èŠã¯ãããŸãã 第4ã«ãããããã¹ãŠã®æ¬åŒ§ã«å®å šã«ããããããŠããå Žåã¯ãæšæºåœ¢åŒã«å€æŽã§ããŸãã
æ··ä¹±ããããš-Aureliaãšã¯å¯Ÿç §çã«ã1åéãã®ææåãšã€ãã³ãå§ä»»ã®å¯èœæ§ã¯ååã§ã¯ãããŸããããããããããã®ããšã¯äžè¬çãªã±ãŒã¹ã§ã¯éèŠã§ã¯ãããŸããã
ã¢ãŠã¬ãªã¢
Aureliaã®ããŒã¿ãã€ã³ãã£ã³ã°ã¯ãããŒã¿ã®ç§»åæ¹åã®åŸ®èª¿æŽã«éç¹ã眮ããŠããŸãããã¬ãŒã ã¯ãŒã¯ã¯ããã€ãã®åççãªä»®å®ãè¡ããŸãããéçºè ã¯èªåã§æ±ºå®ããæ©äŒãæ®ããŸããæçµçã«ã次ã®ãªãã·ã§ã³ããããŸãã
æååè£é
<div class="panel-body"> This string is builted with ${interpolationString} syntax </div>
Angular 2ã®æååè£éãšåæ§ã«ãæ§æã®ã¿ãç°ãªããŸãïŒ{{}}ã§ã¯ãªã$ {}ïŒïŒ
ããããã£ã®ãã€ã³ã
åŒ<property name>ã<Binding type> = "expression"ã«ççž®ãããŸãããã€ã³ãã£ã³ã°ã®ã¿ã€ãã¯æ¬¡ã®ãšããã§ãã
- .bind-é©çšå Žæã«å¿ããŠæ±ºå®ããŸããå ¥åèŠçŽ ã«ã¯åæ¹åãã€ã³ãã£ã³ã°ã䜿çšãããä»ã®ãã¹ãŠã®å Žåã«ã¯äžæ¹åãã€ã³ãã£ã³ã°ã䜿çšãããŸã
- .one-way-Angularã®DOMã«é¡äŒŒ
- .two-way-ã¯ã©ã·ãã¯ãªåæ¹åãã€ã³ãã£ã³ã°
- .one-timeã¯ãå€æŽã远跡ããã«1åéãã®ããŒã¿ã¬ã³ããªã³ã°ãè¡ãããã®ãªãã·ã§ã³ã§ããå¯èœãªéããªãœãŒã¹ãç¯çŽããŸããå žåçãªäœ¿çšäŸã¯ãèªã¿åãå°çšã¢ãŒãã§ããŒã¿ãå«ããªã¹ããæç»ããããšã§ãã
æããã«ãäžè¬ã«ããããã£ãã€ã³ãã£ã³ã°ã®æ§æã¯æ¬¡ã®ãšããã§ãã
<input value.bind="iconUrl" />
Angular 2ã®ããã«æšæºããããã£ããã€ã³ãããããšã«å ããŠãinnerHTMLãtextContentãstyleã®ã«ã¹ã¿ã å±æ§ããããŸãã.bindãè¿œå ããŠããããã£ãæå®ããå Žåãšã.bindã䜿çšããã«æååè£éã䜿çšããå Žåã®2ã€ã®ã¹ãã«ãªãã·ã§ã³ããããŸãã
<div innerhtml.bind="htmlProperty"></div> <div innerhtml="${htmlProperty}"></div>
ã€ãã³ããã³ãã©ãŒ
ã€ãã³ããã³ãã©ããã€ã³ãããã«ã¯ã2ã€ã®ãªãã·ã§ã³ããããŸãã
- .trigger-èŠçŽ ã«çŽæ¥ãã€ã³ãããããã³ãã©ãŒãäœæããŸã
- .delegate â , document ( shadow DOM) , , . . , , bubbling (, , - binding-sample.html).
Angular 2ã®ããã«ããã³ãã©ãŒåŒå ã®ã€ãã³ãã«ã¢ã¯ã»ã¹ããã«ã¯ã$ã€ãã³ãå€æ°ã䜿çšã§ããŸãã
泚ïŒNDCãã³ãã³ã§ã®ã¹ããŒãã§ãã¢ã€ãŒã³ããŒã°ã¯ãAureliaæ§æã¯ãã©ã°ã€ã³ã§ç°¡åã«æžãæããããšãã§ããAngular 2æ§æã䜿çšããŠAureliaã§ã®ããŒã¿ãã€ã³ãã£ã³ã°ã瀺ããŠããŸããããã®ãã©ã°ã€ã³ã¯ã©ãã«ãèŠã€ãããŸããã§ããã
è¯ãã®ã¯ãå¿ èŠã«å¿ããŠåŸ®èª¿æŽã§ããããšã§ãããã€ã³ããªãã·ã§ã³1åãHTMLæšæºãšã®å®å šãªäºææ§ã
é¢åãªã®ã¯ãäžåéããäžæ¹åãåæ¹åã®ãªãã·ã§ã³ã®è©³çŽ°ãªæ§æã§ãããŸããIntelliSenseã®ãµããŒãã¯ãAngularã»ã©ã¯ãŒã«ã§ã¯ãããŸããã
ããŒã¿ãã€ã³ãã£ã³ã°ã®å¶åŸ¡æ§é
è§åºŠ
å¶åŸ¡æ§é ïŒforãifãswitchïŒ
èªè ã¯ãã§ã«ç²ãå§ããŠãããšæãã®ã§ã詳现ã«ã¯è§ŠããŸãããäžè¬çãªæ§æãèŠãŠãã ããïŒ
<div class="panel-body"> Select something: <select [(ngModel)]="selectedClass"> <option *ngFor="#alertClass of alertClasses" [value]="alertClass">{{alertClass}}</option> </select> </div> <div class="panel-body"> <div [ngSwitch]="selectedClass"> <template [ngSwitchWhen]="'success'"> <div class="alert alert-success" role="alert">You will be successfull if you learn Angular</div> </template> ... <template ngSwitchDefault>You must choose option</template> </div> <div *ngIf="selectedClass=='success'"> <div class="alert alert-success" role="alert">Extra message with *ngIf binding</div> </div> </div>
äžè¬çã«ãç°åžžãªããšã¯äœããããŸããã泚æãã¹ãã¯ãngForãšngIfã®æ§æã§ãã*æåããã£ã¬ã¯ãã£ãã®åã«ãããŸããããã¥ã¡ã³ãã§èª¬æãããŠããããã«ãããã¯ãã³ãã¬ãŒãèŠçŽ ã§ãã³ãã¬ãŒããã©ããããããšãé¿ããäžçš®ã®æ§æç³ã§ãã
ãã³ãã¬ãŒãã§ããŒã«ã«å€æ°ãäœæãã
ããŒã«ã«å€æ°ã¯ãhtmlãã³ãã¬ãŒãã®ããŸããŸãªé åã®ããŒã¿ã«ã¢ã¯ã»ã¹ããããã«å¿ èŠã§ããngForã«ãŒãã§æ¢ã«èŠãå€æ°ãäœæããæãç°¡åãªäŸïŒ
<option *ngFor="#alertClass of alertClasses" [value]="alertClass">{{alertClass}}</option>
ç¹æ®æåïŒã䜿çšããŠããã³ãã¬ãŒãå ã§ã¢ã¯ã»ã¹ããå€æ°ã宣èšããŠããããšãAngularã«ç€ºããŸãããçŸåšã®é åèŠçŽ ã®ã€ã³ããã¯ã¹ã瀺ãå€æ°ãäœæããããšãã§ããŸãã
<option *ngFor="#alertClass of alertClasses, #index=index" [value]="alertClass">{{index}} {{alertClass}}</option>
ïŒïŒãŸãã¯æ£èŠã®ä»£æ¿ãval-ãïŒã䜿çšããå€æ°ã®å¥ã®å®£èšã¯ãngForã«ãŒãã®å€åŽã«é©çšã§ããŸãããã®å Žåãå€æ°ã¯å€æ°ã宣èšãããhtmlèŠçŽ ãæããŸãããã®å€æ°ã¯ãããšãã°ã€ãã³ããã³ãã©ãŒã§äœ¿çšã§ããŸãããããããã³ãã¬ãŒãã§ã¯ããã®ãããªå€æ°ã¯æ©èœããŸãããã€ãŸããç£èŠã¯å®è¡ãããŸããã
<input #i placeholder="Type something"> <input type="button" class="btn btn-success" (click)="displayTextboxValue(i.value)" value="And click" /> <br/> But templating doesn't work with it - {{i.value}}
ã¢ãŠã¬ãªã¢
å¶åŸ¡æ§é ïŒforãifãshowïŒ
Aureliaã¹ã€ãããå®è£ ãããŠããªããšããäºå®ããå§ãã䟡å€ããããŸãïŒå°ãªããšãçŸæç¹ã§ã¯ïŒããŸããAureliaã®çšèªã§ã¯ãå¶åŸ¡æ§é ã¯ããŒã¿ãã€ã³ãã£ã³ã°ãæãã®ã§ã¯ãªããå ã»ã©èŠãæ§æã®ããã«HTMLæ¡åŒµãæãããšã«æ³šæããŠãã ããã
å šäœãšããŠãå¶åŸ¡æ§é ãããäŒç€Ÿã®ããã«ããã«æãããŠããç¹°ãè¿ããããã衚瀺ããŸãïŒ
<div class="panel-body"> Select something: <select value.bind="selectedClass"> <option repeat.for="alertClass of alertClasses" value.bind="alertClass">${alertClass}</option> </select> </div> <div class="panel-body"> <div if.bind="selectedClass=='success'" class="alert alert-success" role="alert">You will be successfull if you learn Aurelia</div> ... <div show.bind="selectedClass=='success'"> <div class="alert alert-success" role="alert">Extra message with show extension</div> </div> </div>
ããã«ãrepeatå ã§åŒãèšè¿°ããå Žåã次ã®å€æ°ã䜿çšã§ããŸãã
- $ index-é åå ã®çŸåšã®èŠçŽ ã®ã€ã³ããã¯ã¹
- $ first-ãããé åã®æåã®èŠçŽ ã§ããã°ç
- $ last-ãããé åã®æåŸã®èŠçŽ ã§ããã°ç
- $ even-é åå ã®å¶æ°èŠçŽ ã§ããå Žåã¯true
- $ odd-é åã®å¥æ°èŠçŽ ã®å Žåã¯true
ãã³ãã¬ãŒãã§ããŒã«ã«å€æ°ãäœæãã
äžèšã®repeatå ã§ããŒã«ã«å€æ°ãäœæããããã®æ§æã¯æ¢ã«èŠãŸãããAngular 2ãšæ¯èŒããããã«ãHTMLèŠçŽ ãæãå€æ°ãäœæããæ¹æ³ã確èªããå¿ èŠããããŸããããã«ã¯refå±æ§ã䜿çšãããŸããäœæãããå€æ°ã¯ãã€ãã³ããã³ãã©ãŒãšãã³ãã¬ãŒãã®äž¡æ¹ã§äœ¿çšã§ããŸããã€ãŸããç£èŠãå®è¡ãããŸãã
<div class="panel-body"> <input ref="i" placeholder="Type something"> <input type="button" class="btn btn-success" click.delegate="displayTextboxValue(i.value)" value="And click" /> <br/> And templating works with it - ${i.value} </div>
æ¥ããããããš -æ°é ãã®ããèªè ã¯ãAngular 2ã®éšåã«ãäœãè¯ãã®ã-æ¥ããããã®ãããšããã¡ã¢ããªãããšã«æ°ä»ãããšããããŸããç§ã¯ãã®ã»ã¯ã·ã§ã³ãçµåããäž¡æ¹ã«ã€ããŠæžãããšã«ããŸããããªããªããããã«ã¯1ã€ã®åé¡ãããããã§ãããã®åé¡ã¯ããã³ãã¬ãŒãã«ãšã©ãŒãããå Žåãåžžã«äž¡æ¹ã®ãšã©ãŒãã¹ããŒãããããšã§ã¯ãããŸãããããšãã°ãäž¡æ¹ã®ãã¬ãŒã ã¯ãŒã¯ã®ifã®äŸã§ããããã£åã誀ã£ãŠèšè¿°ããããšãããšãäž¡æ¹ãéãã«åäœãç¶ããŸãã
ãã®ãããç§ãã¡ã¯ééããã¿ã€ããã¹ãæ±ããŠããŸãã
以äžã§ãã èªãã§ãããŠããããšã:)