angularJS 㨠jQuery ã«é¢ãã誤解ã解ã
æè¿ angularJS ã«å¯¾ããæå¾ ã®ä½ä¸ãèããã¦ã¤ããã
ã¿ããªä½¿ãã©ãããééã£ã¦ããããï¼å¹´åããã使ã£ã¦ã¦ä¸æºããããã©èªåã®ãã使ãç¯å²ã§ã¯ angularJS æå¼·ã ã¨æãã
ãªãã ããã 㧠SPA ãã jQuery ã«æ»ã£ã話 - ãã¯ã³ã³
Angularã®åé¡ã§ã¯
ã¯ã¦ãªããã¯ãã¼ã¯ - mizchi ã®ããã¯ãã¼ã¯ - 2015å¹´5æ18æ¥
angularJS ãåãã¦ãã®ã¯ Single Page Application ã§ã¯ãªã
angularJS ãåãã¦ãã®ã¯
â | ãã©ã¼ã ã®ãããªç´°ããé¨åãå¤ç¨ ï¼ DOMããªã¼ã¨ãã¼ã¿ã¹ã³ã¼ããã»ã¼ä¸è´ãã¦ãã¦ã¦ã¼ã¶ã®å ¥åããµã¼ãã«éã£ãããã webã¢ããªã管çç»é¢ããã¤ãã¼ã¸ãæ¥åã¢ããªãªã© |
â³ | Single Page Application â ç°¡åã«ä½ãããã©ãã¼ã¸éã®é£æºãå¿ è¦ãªããªããµã¼ãå´ã§åãã¦ãã¾ã£ãæ¹ãããã |
à | SEO対çãå¿ è¦ãªãã¼ã¸ãSEOã大äºãª webãµã¼ãã¹ã®ããã³ãå´ã¨ã |
à | ã²ã¼ã ã®ããã«DOMããªã¼ã¨ãã¼ã¿ã¹ã³ã¼ããããã¾ãé¢ä¿ãªããã®ãå¤åãããã¼ã¿ãå¤ãã¦fpsåä½ã§ã®ã¹ãã¼ããæ±ãããããã® |
ã¨ã㫠管çç»é¢ã«ãããã¡ãªãä¸è¦§ï¼ä¸¦ã³æ¿ã/çµãè¾¼ã¿/表示形å¼å¤æ´ï¼ã»ç·¨éã»è¿½å ã»åé¤ ã¨ããä¸é£ã®ç»é¢ã®ä½ããããã¯ããããä¸æããã¨ã»ã¨ãã©javascriptæ¸ããã«å®è£ ã§ãã*1ã
ããã¦ãjavascriptã¢ããªã±ã¼ã·ã§ã³ã®ãµã¤ãºãã¯ãªãã¹ãå°ããåä½ã«ã¨ã©ãã¦ãããã¨ããè¦ããããä¾ãã°ã管çç»é¢ã®ãã°ã¤ã³ããã·ã³ã°ã«ãã¼ã¸ã¢ããªã±ã¼ã·ã§ã³ãã¨ããã®ã¯é¿ããæ¹ãããã¦ããã°ã¤ã³ã¯javascriptãªãã®ãã©ã¼ã ããã®å¾ã¡ãã¥ã¼ãããåå管çç»é¢ã«é·ç§»ãããããã§ä¸ã¤ã®ã¢ããªãã®ããã«æ®éã®ãã¼ã¸é·ç§»ï¼å¥ã®mainã³ã³ããã¼ã©ã¼ã¨ããæ§æããæ±ããããå¯è½ãªããä¸è¦§ãã詳細編éãã§å¥ã«ããã¨ãã£ã¨åãåããå¹ãã
è¤æ°ã®ãã¼ã¸ã管çãããã¨ãããã¨ã¯ãç¶æ
ã示ãå¤æ°ã大ããè¤éã«ãªãã¨ãããã¨ã ãç¶æ
ãè¤éã«ãªãã¨ãå®è£
ã¯ããã«ææ°é¢æ°çã«è¤éã«ãªããé度ãé
ããªãããã¹ãã大å¤ã«ãªããã§ãããªãå°ããããæ¹ãå½±é¿ç¯å²ãçããªã£ã¦æ¹ä¿®ãããããã¡ã¢ãªãªã¼ã¯ãªã©ãèµ·ãã£ã¦ãå½±é¿ãå°ãªãã¦ããããå¥ã¦ã¼ã¶ã®æä½ããããå¦çãªã©ãããããããµã¼ãã®ãã¼ã¿ã¨ã®ã³ã³ããªã¯ãã®å¯è½æ§ãæ¸ãã
ç¹ã«ããã©ã¼ãã³ã¹ãæ±ããããªãéããã·ã³ã°ã«ãã¼ã¸ã¢ããªã±ã¼ã·ã§ã³ãããããã¼ã¸é·ç§»ããã¦ããããªããã«ããæ¹ããããããåæåãããã®ã§éçºã楽ã§ãã¦ã¼ã¶ä½é¨ãèªç¶ãªãã¨ãå¤ãã
ãã㦠angularJS ã§ä½ã£ã¦ããã¨ããä¸è¦§ãã詳細編éãã§å¥ã«ãã¦ããã©ããããä»æ§è¿½å ããã£ã¦ä¸ã¤ã«ã¾ã¨ããã»ãã便å©ãçãªæµãã§ãçãæ®ãããããã
(追è¨ï¼5/20)ãangularJS ã¯ã·ã³ã°ã«ãã¼ã¸ã¢ããªã±ã¼ã·ã§ã³ã«åãã¦ãªããã¨ããäºã§ã¯ãªãã¦ãã·ã³ã°ã«ãã¼ã¸ã¢ããªã±ã¼ã·ã§ã³ã£ ã¦ããããé£ãããããé¿ãããããªãé¿ããã»ããéçºã楽ãã¨ãã話ã§ã(/追è¨)
angularJS 㨠jQuery ã¯åæã«ä½¿ã£ã¦ãã
angularJS 㨠jQuery ã¯ã¾ãã«ã©ã¤ãã©ãªã¨ãã¬ã¼ã ã¯ã¼ã¯ã®é¢ä¿ã§ãangularJS ã®DOMæä½é¨å=ãã£ã¬ã¯ãã£ãã®ä¸ã§ãjQuery ãåããããã«ããã°ããããããããªè§£èª¬ã§ãjQuery使ã£ã¡ããã¡ãã¿ãããªäºãæ¸ãã¦ãããããã³ã³ããã¼ã©ã¼ããDOMãã»ã¬ã¯ã¿æå®ã§ã¤ãã³ãä»è¾¼ããããªããæ¹ã§ jQuery ã¯ä½¿ã£ã¡ããã¡ãã¨ããã®ãæ£ããããªããªããDOMã®çæç ´æ£ã®ã¿ã¤ãã³ã°ãã³ã³ããã¼ã©ã¼ã®å®è¡ã¿ã¤ãã³ã°ã¨éããããã ã
ãµã¼ãã¹ã§ã¯ã¨ã¬ã¡ã³ãæå®ããªããªã使ã£ã¦ãããããã£ã¬ã¯ãã£ãã§ã¯ããã®ãã£ã¬ã¯ãã£ãå
ã«å½±é¿ç¯å²ãã¨ã©ãããã¨ãã注æãæã£ã¦ããã°ç©æ¥µçã«ä½¿ã£ã¦ãããï¼scope.$apply ãªã©ã¨ã®é£æºæ¹æ³ã®å¦ç¿ãå¿
è¦ã«ãªããï¼ã
jQuery ãã©ã°ã¤ã³ã«ã¯ãããããã£ã¦ãangularJS ã¨çµã¿åãããã¨ãã®ç¸æ§ã»æéã¯
â | HTML/DOM/ã¤ãã³ãã«é¢ä¿ãªãã©ã¤ãã©ãª jQuery.md5 ã¨ã | âãã©ãã§ã(ã³ã³ããã¼ã©ã¼å ã§ãã)ç©æ¥µçã«ä½¿ã£ã¦ãããã¾ãããjQueryã§ããå¿ è¦ãªããã⦠|
â | å¤é¨ãµã¼ãã¹ã¨ã®éä¿¡ã©ã¤ãã©ãª $.ajax ã¨ãã | âããµã¼ãã¹ã«ããããããã¦åä¿¡ãå®äºããã promise ã§ã³ã¼ã«ããã¯ãã¦ã $rootScope.$apply() ãå¼ã¶ |
â | $(elm).hoge() ãã㨠elm ã«ã¨ãã§ã¯ããããããã®ï¼text-shadow ã¨ãï¼ã | â ç°¡åã«ãã£ã¬ã¯ãã£ãåã§ããã®ã§ãããã£ã¦ä½¿ãã¾ãããã |
â | $(elm).hoge() ãã㨠elm ã®å é¨ã®domãå¤æ´ãã¦ãªããããç³»(ã¹ã¯ãã¼ã«ãã¼ãããããã«ã¨ã) | â ã¾ãã ããããã£ã¬ã¯ãã£ãåã§ããã®ã§ãããã£ã¦ä½¿ãã¾ããããä¸é¨ dom ãå£ã㦠angularJS ãè¨å®ãã dom æ å ±ã¨ãç ´å£ãããã®ããããããããªãã®ã§ããã ã注æ |
â | $(elm).hoge() ãã㨠elm ã®å é¨ã«ãã¼ã¿ã«æ²¿ã£ããã¼ãã表示ãããç³»(ã«ã¬ã³ãã¼è¡¨ç¤ºã¨ãã°ã©ãç³»ã¨ã) | â ç°¡åã«ãã£ã¬ã¯ãã£ãåã§ããã®ã§ãããã£ã¦ä½¿ãã¾ãããããã©ã¼ã ã®å ¥åãã¼ããªã ngModel ã¨ãã¨é£æºããªãã¨ãããªãã¦ãã®è¾ºãã¡ãã£ã¨è¤éã ãã©å°ãé å¼µãã°ã§ãã |
â³ | ãã³ãã¬ã¼ãã¨ã³ã¸ã³ | â ãã£ã¬ã¯ãã£ãåã§ãããã©å®å ¨ã«angularJSåããæ¹ããããã |
ã¨ããæãã *2ãä»ã® jQuery 以å¤ã®ã©ã¤ãã©ãªã§ãã ãããåãæãã§é£æºã§ããã
jQuery ã angularJS ã®ãã£ã¬ã¯ãã£ãã«éãè¾¼ããã®ã¯é£ãããªã
angularJS ã®ä¸ã§ããã£ã¬ã¯ãã£ãã®ä»æ§ã¯è¤éæªå¥ã§ãããããjQueryãéãè¾¼ãããã¨ããç®çãªããããªã«é£ãã使ãæ¹ã¯å¿ è¦ãªãã
ä¾ãã°
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script> <button class="popup" data-text="hoge1">1</button> <button class="popup" data-text="hoge2">2</button> <div id="alert" style="display:hidden"></div> <script> $(function(){ /** * popup ã¯ã©ã¹ã®ãã¿ã³ã¯ãããã¢ãããã¯ãªãã¯ããã¨ã¢ã©ã¼ãdiv(#alert)ã表示ããã表示å 容㯠ãã¿ã³ã® data-text ã®å 容ã */ $("button.popup").click(function(){ $("#alert").hide().text($(this).data("text")).fadeIn(); }); }); </script>
ã¨ããã®ãããã¨ãã*3ããããªã
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularJS/1.2.16/angular.js"></script> <div ng-app="app"> <button popup data-text="hoge1">1</button> <button popup data-text="hoge2">2</button> <div id="alert" style="display:hidden"></div> </div> <script> angular.module('app') /** * popup ãã£ã¬ã¯ãã£ãã¯ãããã¢ãããã¯ãªãã¯ããã¨ã¢ã©ã¼ãdiv(#alert)ã表示ããã表示å 容㯠ãã¿ã³ã® data-text ã®å 容ã */ .directive('popupDirective',function(){ return { link:function(scope, elm, attrs){ elm.click(function(){ $("#alert").hide().text(elm.data("text")).fadeIn(); }); } }; }); </script>
ã¨ãã¨ãããã使ããã®ã¯ããã«ä½ããã #alert ãããã¾ãã«æ°ã«ãªãï¼é¨åã®ã¹ã¯ãªããã«å ¨ä½ã®ç¹æ®äºæ ãæ¸ããã¦ããï¼ï¼ããã¾ã
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularJS/1.2.16/angular.js"></script> <div ng-app="app"> <button popup popup-target="#alert" data-text="hoge1">1</button> <button popup popup-target="#alert" data-text="hoge2">2</button> <div id="alert" style="display:hidden"></div> </div> <script> angular.module('app',[]) /** * popup ãã£ã¬ã¯ãã£ãã¯ãããã¢ãããã¯ãªãã¯ããã¨ã¢ã©ã¼ãã表示ããã * 表示å 容㯠ãã¿ã³ã® data-text ã®å 容ã * popup-target å±æ§ã«ã¢ã©ã¼ãããã¯ã¹ã®ã»ã¬ã¯ã¿ãæå®ãã(ä¾ï¼ popup-target="#alert" )ã */ .directive('popup',function(){ return { link:function(scope, elm, attrs){ elm.click(function(){ $(attrs.popupTarget).hide().text(elm.data("text")).fadeIn(); }); } }; }); </script>
ããããã¨ããã¡ãã£ã¨æ±ç¨çã«ãªãï¼å ã®jQueryã®ãã¤ããæ±ç¨çã«ãªã£ãï¼ï¼ãã ãããã® jQuery ããã°ã©ã ã¯ããããªæãã§èªåã§ä¸éå端ã«ã©ãããã¦ä½¿ãã¨ãjQuery ã®å½±é¿ç¯å²ãå°ãè¾¼ãããã¨ãã§ããã*4
ã¯ã¾ããã¡ãªã®ã¯
- ç¬èªã®ã¿ã¤ãã³ã°ã§ scope ã®å¤ãæ´æ°ãããã©è¡¨ç¤ºãæ´æ°ãããªã
- scope.hoge=huge *5 ã¿ããã«æ´æ°ããå¾ã«ã scope.$apply ãå¼ã°ãªãã¨æ´æ°ããã¾ãã
- éã«ãããã£ã¬ã¯ãã£ãã«ãã£ã¦å¤ãªã¨ããã§åã¹ã³ã¼ããä½æããã¦ã¦åã¹ã³ã¼ãã«å¤ã追å ãã¦ãããã â scope.hoge ãããªãã¦scope.hoge.huge ã®ããã«ãªãã¸ã§ã¯ããä»ãã¦æ´æ°ããããâ¦â¦(ããããã¦ãã¦)
- jQuery ã§ãã£ã¦ãã¢ãã¡ã¼ã·ã§ã³ã ng-animate ã§ã©ããã£ãããããåãããªã
- åºãæ¶ããããã¨ã¬ã¡ã³ããåçã«çæãããã¨ãã¦èºã
- â jQuery ã® show() hide() ã¨ã ng-show ng-if ã§ããããã§
- ãjQuery éä¾åãã謳ã angularJSå°ç¨åæ©è½ã¢ã¸ã¥ã¼ã«ãæ¢ãããã©ä»ã¾ã§ã¨æ¸ãæ¹ãã ãã¶å¤ãã£ã¦ã¤ãã
- â æ¦å¿µ/使ãæ¹ãå ¨ç¶éã£ã¦å¦ç¿/æ¸ãæãã³ã¹ããé«ããã¨ãå¤ãããããã§è¦ã¤ããã®ã¯çç³æ··åã§ãã®ã¼ã ã£ããããããæåã®å ã¯èªåã§ãã£ã¬ã¯ãã£ãä½ã£ã¦ jQuery ãã©ã°ã¤ã³ãä¸éå端ã«ã©ãããã¦ä½¿ã£ãã»ãã楽ãç®å½ã¦ã® jQuery ãã©ã°ã¤ã³ããªã㦠angularJS ã¢ã¸ã¥ã¼ã«ãç¥ã£ã¦ããªã angularJS å°ç¨ã¢ã¸ã¥ã¼ã«ã®ä½¿ãæ¹ãè¦ãããã§ãããã©ããã§ã«ä½¿ãæ £ãã jQuery ãã©ã°ã¤ã³ããããªãèªåã®ä½¿ãæ¹ã«åããã¦ã©ããããããã®åèã«æ¢åã®ã¢ã¸ã¥ã¼ã«ãæ¢ãã¨ãããã«ãªãï¼ãªã¬ãªã¬ã©ããã¼ã«ããããæ©è½ã»æ±ç¨æ§ã足ãã¦ããã¨æçµçã«ããªãã»ã©ï¼ãã®ã¢ã¸ã¥ã¼ã«ãããèãããã¦ããã ï¼ãã¨ãªããã¨ãå¤ãï¼
- scope.$on()ãscope.$emit ãªã©ã§ç¬èªã¤ãã³ããçºè¡ãããããã¤ãã³ãé§å
- â ã¤ãã³ããã°ãã¼ãã«å¤æ°ã«ãªããã¡ãã¤ãã³ããªã¹ãå´ã¯ scope.$watch ãªã©ã§å¤æ°ç£è¦ã ã¤ãã³ãçºè¡å´ã¯ scope ã®å¤æ°ãå¤æ´ã㦠scope.$applyã ç£è¦ããå¤æ°åã attrs ã§æå®ã§ããããã«ããã scope.$eval ã $parse ãªã©ã§å¼ãå®è¡ã»ä»£å ¥ã§ããããã«ãªãã¨æ±ç¨æ§ãä¸ãããéåæçµæåå¾ã®å ´å㯠$q ( promise ) ã使ã£ãã³ã¼ã«ããã¯
æ¢åã® jQuery ã idãã»ã¬ã¯ã¿æå®ã¤ããã¾ããã®å ´åã¯ç§»è¡ãå³ãããããããããã®ç¶æ
ãå¶éãããï¼idãã»ã¬ã¯ã¿ãã°ãã¼ãã«å¤æ°çã«ãªããã¡ãªã®ã§åé¿ãããï¼ãããã®ããã£ã¬ã¯ãã£ãåãã§ããã®ã§ããã¯ç©æ¥µçã«å¤ãã¦ãããã
åæã³ã¹ããå¢ãã¦ãã¡ãªãããããå ´åã«ãã¬ã¼ã ã¯ã¼ã¯ãå°å ¥ãã
jQuery é¢ä¿ããã£ã¬ã¯ãã£ãã»ã¹ã³ã¼ãã«åãåºãã¡ãªããã¨ãã¦ã¯ãangujarJS ã®ä»ã®æ©è½ã使ãããã®ä»ã«
- view ã®å¤æ´ã¨ãã¼ã¿ã®å¤æ´ãåãé¢ãã
- ãã¹ãã容æã«ãªã
- æ±ç¨æ§ãä¸ãã
- ã¡ã³ããã³ã¹æ§ãä¸ãã
- view ã®å¤æ´ã®å½±é¿ç¯å²ãçãããã
- åå©ç¨æ§ãä¸ãã
代ããã«
- 使ãåãããªãé¨åã§ããã£ã¬ã¯ãã£ãåãæ±ãããã
- è¦ããªãã¨ãããªãè¦åãå¤ããªã
ä¸ã®ä¸ã®webéçºã«ã¯ãããã®ã³ã¹ããè¦ãªãæ¹ãç¾å®ã«ãã£ã¦ããå ´åãå¤ãã¦ãä¾ãã°åºåã»çæã¤ãã³ããã¼ã¸ãªã©æ¹ä¿®ã»é·æã¡ã³ããã³ã¹ãæ³å®ãããªãã£ãããç¶æ
ãå°ãªããå
¨é¨ã°ãã¼ãã«å¤æ°ã§ãä½ã¨ããªããç¨åº¦ã®è¤éãã®ãã¼ã¸ã ããã®ã°ããã¯jQuery ã®æ¹ãåã£ã¦ããã ããã
ããã¯ãã¢ã¸ã¥ã¼ã«åãã¹ããï¼ãããã¬ã¼ã ã¯ã¼ã¯ãå°å
¥ããã¹ããï¼ãããã¤ã¯ããµã¼ãã¹ã«åå²ãã¹ããï¼ãã¿ãããªåé¡ã§ãPHPã§ãåãã¡ã¤ã«ã«ã¹ãæ¸ããããå
±éincludeããæ¹ãããè¦æ¨¡ããããrubyãªã©ã¯ãã³ã¼ããã¨ã©ã¼å¦çãcgiè¨å®ãªã©è«¸ã
ãã¦ããå
ã«ããªãrails/sinatraã§ãã¨ãªããã¡ã ã PHP ãªã 3ãã¼ã¸ãããã§ä»ã¨åãåãããããªãç´ ã®PHPã®æ¹ã楽ã§æ©ããã¿ãããªè©±ã ã
ã¾ã¨ã
- angularJS ãåãã¦ãã®ã¯ Single Page Application ã§ã¯ãªã
- angularJS 㨠jQuery ã¯åæã«ä½¿ã£ã¦ãã
- jQuery ã angularJS ã®ãã£ã¬ã¯ãã£ãã«éãè¾¼ããã®ã¯é£ãããªã
- åæã³ã¹ããå¢ãã¦ãã¡ãªãããããå ´åã«ãã¬ã¼ã ã¯ã¼ã¯ãå°å ¥ãã
- angularJS æå¼·ãªã®ã§ä½¿ãã¾ãããã
*1:ã¾ãããã«ã¤ãã¦ãããjavascriptãªãã¦ãã§ããããã¨ããã®ã¯åæã§ãããã§ååãªãjavascript使ããªãæ¹ãããã¨æãã¾ã
*2:俺調ã¹ãããã¡ãã£ã¨ç·´ã£ããããããããæéãåºããã®ããã誰ãâ¦â¦
*3:ãã®ç¨åº¦ã ã¨ããããä½ããªãã¦ãæ¢åã®ãã£ã¬ã¯ãã£ãã§å®è£ ã§ããããã¾ã jQuery 使ãä¾
*4:æ¬å½ã¯ããä¸æ®µéé²ã㦠ããããã ã¾ã§ããã¨ãããã« angularJS ã£ã½ãã¦ããªãã»ã© ng-click ng-show ã£ã¦ãããªã£ã¦ãã®ããã¿ãããªã®ãè¦ãã¦ããã¨æã
*5:ããã㯠scope.$eval ã $parse(exp)(scope).assign(val) ãªã©