AngularJSéçºè ã®ããã®AngularDart
AngularDartã¯ãããå¢ãã§é²åãã¦ãã¦ãä»ã®AngularDartã¯ãã®è¨äºã¨ã¯ããã¶ãéã£ã¦ãã¾ããå è¨äºã®å 容ã¯ææ°çã«åããã¦æ¸ãæãããã¦ããã®ã§åãæ¥ããã¡ããè¦ãã»ããããã§ããæéãåããã訳æãç´ãã¾ãã(2014-06-06追è¨)
ã¿ããªå¤§å¥½ãangular.jsã®dartçãangularãã¼ã ã«ãã£ã¦çµ¶è³éçºä¸ãªãã§ããããããåãªãJSçã®ç§»æ¤ã§ã¯ãªãã¦ãã¡ããã¨dartç¨ã«æ°ãã«ç·´ããªããã¦ãæããªããã§ãã
ãããªAngularDartãAngularJSã¨æ¯è¼ããç´¹ä»è¨äºãããã£ãã®ã§ã¡ãã£ã¨è¨³ãã¦ã¿ã¾ãããå人çã«ã¯Dartçã®æ¹ãé»éè¡åãæ¸ã£ã¦ã¦ããæããªããããªããã¨ã
è¨äºã«æ¸ãã¦ãããã©ãAngularDartã®æ©è½ã®å¹¾ã¤ãã¯AngularJSã«éè¼¸å ¥ããããããã®ã§ãDartãªããç¥ãããã£ã¦äººãèªãã§ããã¦æã¯ãªãããã
http://victorsavkin.com/post/72452331552/angulardart-for-angularjs-developers-introduction-to
AngularJSéçºè ã®ããã®AngularDart - æé«ã®Angularã®ç´¹ä»
AngularDartã¯é«ãè©ä¾¡ãåãã¦ãããã¬ã¼ã ã¯ã¼ã¯ã®Dartãã©ãããã©ã¼ã ã¸ã®ç§»æ¤çã§ãAngularã³ã¢ãã¼ã ã«ãã£ã¦éçºããã¦ãã¾ãããã®è¨äºã§ã¯ãã®ãã¬ã¼ã ã¯ã¼ã¯ã®Dartçã¨JSçãæ¯è¼ãã¾ããä¸ã§ãç¹ã«ä¾åæ§ã®æ³¨å ¥ããã£ã¬ã¯ãã£ããdigestingã«ã¤ãã¦è©³ããè¿°ã¹ã¾ãã
æ³å®ãããèªè
ãã®è¨äºã¯æ¬¡ã®ãããªäººã«åãã¦æ¸ããã¦ãã¾ã:
- ããããã®AngularJSã®çµé¨ãæã£ãDartéçºè ã
- AngularDartã試ãã¦ã¿ããã¨èãã¦ããAngularJSéçºè ã
- Dartã«åãæ¿ããã¤ããã¯ãªããããã®ãã¬ã¼ã ã¯ã¼ã¯ã®æªæ¥ã«ã¤ãã¦ç¥ãããã¨æã£ã¦ããAngularJSéçºè ãAngularéçºé£ã«ããã¨ãAngularDartã®æ©è½ã®å¤ãã¯å°æ¥çã«AngularJSã«ç§»æ¤ãããããã§ãããã®ããããã®ãã¬ã¼ã ã¯ã¼ã¯ã®Dartçã«ã¤ãã¦å¦ã¶ãã¨ã¯ãå®éã«ä½¿ãäºå®ããªãã¨ãã¦ãèå³æ·±ãæãããããã¨ã§ãããã
ä¾åæ§ã®æ³¨å ¥
ååã«åºã¥ããæ³¨å ¥ã¨åã«åºã¥ããæ³¨å ¥
AngularDartã¯Dartã®ãªãã·ã§ãã«ãªåã·ã¹ãã ãèå³æ·±ãå½¢ã§å©ç¨ãã¦ãã¾ããã¤ã¾ããåæ å ±ãã¤ã³ã¸ã§ã¯ã¿ãè¨å®ããããã«å©ç¨ãã¾ããè¨ãæããã¨ãæ³¨å ¥ã¯ååã§ã¯ãªãåã«åºã¥ãã¦å®è¡ããã¾ãã
//JS: // ããã§ãååã¯éè¦ã§ããæ¬çªç°å¢ã§minifyãããã®ã§ // ãã®ããã«é åã使ç¨ããªããã°ããã¾ããã m.factory("usersRepository", ["$http", function($http){ return { all: function(){/* ... */} } }]);
//DART: class UsersRepository { // ããã§ã¯åã ããéè¦ã§ããå¤æ°åã¯DIã¨ç¡é¢ä¿ã§ãã UsersRepository(Http h){/*...*/} all(){/* ... */} }
æ³¨å ¥å¯è½ãªãªãã¸ã§ã¯ãã®ç»é²
AngularJSã§ã¯æ³¨å ¥å¯è½ãªãªãã¸ã§ã¯ãã¯ãfilter, directive, controller, value, constant, service, factory, providerãªã©ã®ã¡ã½ããã使ç¨ãããã¨ã§Angular DIã·ã¹ãã ã«ç»é²ã§ãã¾ãã
ã¡ã½ãã | ç®ç |
---|---|
filter | ãã£ã«ã¿ã¼ã®ç»é² |
directive | ãã£ã¬ã¯ãã£ãã®ç»é² |
controller | ã³ã³ããã¼ã©ã¼ã®ç»é² |
value, constant | è¨å®ãªãã¸ã§ã¯ãã®ç»é² |
service, factory, provider | ãµã¼ãã¹ã®ç»é² |
è¦ã¦åããéãã注å
¥å¯è½ãªãªãã¸ã§ã¯ããç»é²ããæ¹æ³ãããã¤ããããã¨ãéçºè
ã®æ··ä¹±ã®ç¨®ã«ãªã£ã¦ãã¾ãããã²ã¨ã¤ã«ã¯ãfilterãdirectiveãcontrolleré¢æ°ã¯ãã¹ã¦åã®ç°ãªããªãã¸ã§ã¯ãã®ããã«å©ç¨ãããããã交æå¯è½ã«ã¯ã§ããªãã£ãã¨ãããã¨ãããã¾ãããããä¸æ¹ã§ãserviceãfactoryãprovideré¢æ°ã¯ï¼providerãæãæ±ç¨çã§ããï¼ãã¹ã¦ãµã¼ãã¹ãªãã¸ã§ã¯ããç»é²ãããã®ã§ãã
AngularDartã¯ããã¨ã¯å ¨ãç°ãªãã¢ããã¼ããåãã¾ããããªãã¸ã§ã¯ãã®åã¨DIã·ã¹ãã ã«ã©ã®ããã«ç»é²ãããããåé¢ããã®ã§ãã
valueãtypeãfactoryã使ç¨ããã¨ä»»æã®ãªãã¸ã§ã¯ããç»é²ã§ãã¾ãã
ã¡ã½ãã | ç®ç |
---|---|
value, type, factory | ãããããªãã¸ã§ã¯ãã®ç»é² |
ããã¯æ¬¡ã®ããã«å©ç¨ããã¾ãã
//DART: // 渡ããããªãã¸ã§ã¯ãã¯æ³¨å ¥ã«å©ç¨ã§ãã¾ãã value(UsersRepositoryConfig, new UsersRepositoryConfig()); // AngularDartã¯ãã¹ã¦ã®ä¾åæ§ã解決ã㦠// UsersRepositoryãã¤ã³ã¹ã¿ã³ã¹åãã¾ãã type(UsersRepository); // AngularDartã¯factoryé¢æ°ãå¼ã³åºãã¾ãã // 渡ãããã¤ã³ã¸ã§ã¯ã¿ã使ç¨ãã¦ä¾åæ§ã解決ããå¿ è¦ãããã // ãã®å¾ãUsersRepositoryãã¤ã³ã¹ã¿ã³ã¹åãã¾ãã factory(UsersRepository, (Injector inj) => new UsersRepository(inj.get(Http)));
ãããã®é¢æ°ã使ç¨ããã¨ã©ã®ãããªãªãã¸ã§ã¯ãã§ãç»é²ã§ãããããAPIãé常ã«åç´ã«ãªã£ã¦ãã¾ãã
ä»»æã®ã¯ã©ã¹ããµã¼ãã¹ã¨ãã¦å©ç¨ã§ãã¾ããåã«Augular DIã·ã¹ãã ãå©ç¨ãã¦ç»é²ããã ãã§æ§ãã¾ãããå¿ è¦ã«ãªã£ãæã«Angularããã®ã¯ã©ã¹ãã¤ã³ã¹ã¿ã³ã¹åãã¦ãã³ã³ã¹ãã©ã¯ã¿å¼æ°ãéãã¦ãã¹ã¦ã®ä¾åæ§ãæ³¨å ¥ãã¾ãã
ããããä»ã®åã®ãªãã¸ã§ã¯ãã«ã¤ãã¦ã¯è¿½å ã®æ å ±ãä¸ããå¿ è¦ãããã¾ããããã«ã¯ã¢ããã¼ã·ã§ã³ã使ç¨ãã¾ãã
//DART: @NgController( selector: '[users-ctrl]', publishAs: 'ctrl' ) class UsersCtrl { UsersCtrl(UsersRepository repo); }
åæ§ã«ãfilterãcomponentãdirectiveãå®ç¾©ããããã®ç¹å¥ãªã¢ããã¼ã·ã§ã³ãããã¾ãã
AngularDartã§ã¯æ³¨å ¥å¯è½ãªãã¸ã§ã¯ãã®åã¨ãã©ã®ããã«ããããDIã·ã¹ãã ã«ç»é²ããããã¨ãããã¨ã¯ãç´äº¤ããæ¦å¿µã§ãã
ã¢ã¸ã¥ã¼ã«ã®ä½æã¨ã¢ããªã±ã¼ã·ã§ã³ã®éå§
以ä¸ã¯AngularJSã§ã¢ããªã±ã¼ã·ã§ã³ãä½æããæ¨æºçãªæ¹æ³ã§ãã
//JS: var m = angular.module("users", ['common.errors']); m.service("usersRepository", UsersRepository); angular.bootstrap(document, ["users"]);
ããã¯AngularDartã«ã¯æ¬¡ã®ããã«å¯¾å¿ä»ãããã¾ãã
//DART: final users = new Module() ..type(UsersRepository) ..install(new CommonErrors()); ngBootstrap(module: users);
åæ§ã®ãã¨ãModuleãç¶æ¿ãããã¨ã«ãã£ã¦ãå®ç¾ã§ãã¾ãã
//DART: class Users extends Module { Users(){ type(UsersRepository); install(new CommonErrors()) } } ngBootstrap(module: new Users());
å®è¡ä¸ã®ãã©ãããã©ã¼ã ãã¢ããªã±ã¼ã·ã§ã³ã«å¿ãã¦ç°ãªãã³ã³ãã¼ãã³ãã¨ç´ä»ããããããªå ´åã«ã¯å¾è
ã®æ¹æ³ãããé©åã§ãããã
æ³¨å ¥å¯è½ãªãã¸ã§ã¯ãã®è¨å®
AngularJSã¯æ³¨å ¥å¯è½ãªãã¸ã§ã¯ããè¨å®ããããã®ãªãã·ã§ã³ãããã¤ãæä¾ãã¦ãã¾ãããã£ã¨ãåç´ãªæ¹æ³ã¯valueã使ç¨ãã¦è¨å®ç¨ã®ãªãã¸ã§ã¯ããæ³¨å ¥ãããã¨ã§ãã
//JS: m.value("usersRepositoryConfig", {login: 'jim', password: 'password'}); m.service("usersRepository", function (usersRepositoryConfig){ //... });
åæ§ãªãã¨ã¯Dartã§ãå¯è½ã§ãã
//DART: class UsersRepositoryConfig { String login; String password; } class UsersRepository { UsersRepository(UsersRepositoryConfig config){/* ... */} } type(UsersRepository); value(UsersRepositoryConfig, new UsersRepositoryConfig()..login="Jim"..password="password");
ã§ã¯ããã§UsersRepositoryã¯hashã§ã¯ãªãå¼æ°ã2ã¤åããããã¯å¤æ´ã§ããªããã®ã¨ãã¾ãããããã®å ´åã¯ãfactoryã使ç¨ãããã¨ãã§ãã¾ãã
//JS: m.value("usersRepositoryConfig", {login: 'jim', password: 'password'}); m.factory("usersRepository", function (usersRepositoryConfig){ return new UsersRepository(usersRepositoryConfig.login, usersRepositoryConfig.password); });
AngularDartçã§ããåã³é常ã«ããä¼¼ãã³ã¼ãã«ãªãã¾ãã
//DART: value(UsersRepositoryConfig, new UsersRepositoryConfig()..login="Jim"..password="password"); factory(UsersRepository, (Injector inj){ final c = inj.get(UsersRepositoryConfig); return new UsersRepository(c.login, c.password); });
ãã®ãããªç®çã§ã¯providerãå®ç¾©ãããã¨ã好ã人ãããã§ãããã
//JS: m.provider("usersRepository", function(){ var configuration; return { setConfiguration: function(config){ configuration = config; }, $get: function($modal){ return function(){ return new UsersRepository(configuration); } } }; });
setConfigurationã¡ã½ããã¯ã¢ããªã±ã¼ã·ã§ã³ã®è¨å®ãã§ã¼ãºä¸ã«å¼ã³åºãããªããã°ããã¾ããã
//JS: m.config( function(usersRepositoryProvider){ usersRepositoryProvider.setConfiguration({login: 'Jim', password: 'password'}); } );
AngularDartã«ã¯providerãæ示çãªè¨å®ãã§ã¼ãºããªããããä¸è¨ã®ä¾ãç´æ¥Dartã«ç½®ãæãããã¨ã¯ã§ãã¾ãããæãã¤ãæãè¿ãä¾ã¯æ¬¡ã®ãããªãã®ã§ãã
//DART: final users = new Module()..type(UsersRepositoryConfig) ..type(UsersRepository); Injector inj = ngBootstrap(module: users); inj.get(UsersRepositoryConfig)..login = "jim" ..password = "password";
directiveãcontrollerãcomponent
ããã§ã¯è©±é¡ãå¤ãã¦ããã¬ã¼ã ã¯ã¼ã¯ã®ããä¸ã¤ã®æ±ã directiveã«ã¤ãã¦è©±ãã¾ãããã
AngularJSã®directiveã¯é常ã«å¼·åã§åºæ¬çã«ã¯ä½¿ãããããã®ã§ãããæ°ããdirectiveãå®ç¾©ãããã¨ãæ··ä¹±ã®å ã«ãªãå ´åãããã¾ããæãã«ãAugularãã¼ã ãããã¯èªèãã¦ãã¦ãããããã®ãã¬ã¼ã ã¯ã¼ã¯ã®Dartçã§ã¯APIã大ããç°ãçç±ãªã®ã§ãããã
AngularJSã«ã¯UIã®ã¤ã³ã¿ã©ã¯ã·ã§ã³ãã¾ã¨ããããã®ãªãã¸ã§ã¯ãã2ã¤ããã¾ã:
- directiveã¯DOMã¨ã®ãã¹ã¦ã®ããã¨ããã«ãã»ã«åãã¾ããããã¯å®£è¨çã§ãhtmlãæ¡å¼µããæ段ã¨ã¿ãªããã¨ãã§ãã¾ãã
- controllerã¯å½ä»¤çã§ããDOMã«ã¤ãã¦ã¯æèãããã¢ããªã±ã¼ã·ã§ã³ã®ãã¸ãã¯ãæ±ããã¨ãã§ãã¾ãã
AngularJSã§ã¯ããã2種é¡ã®ãªãã¸ã§ã¯ããåºå¥ããã¦ãã¾ããããããç»é²ããããã«ç°ãªããã«ãã¼ãå©ç¨ãããå ¨ãç°ãªãAPIã使ç¨ãã¦å®ç¾©ããã¾ãã
AngularDartã«å°å ¥ããã1ã¤ç®ã®é大ãªå¤æ´ã¯ãããã2ã¤ã®ãªãã¸ã§ã¯ããããä¼¼éã£ããã®ã«ãªã£ãã¨ãããã¨ã§ããcontrollerã¯åºæ¬çã«è¦ç´ ã«æ°ããã¹ã³ã¼ããä½æããdirectiveã§ãã
2ã¤ç®ã®å¤æ´ã¯æ°ãããªãã¸ã§ã¯ãåãcomponentã§ããAngularDartã§ã¯ãdirectiveã¯ã»ã¨ãã©ã®å ´åã§æ¢åã®DOMè¦ç´ ãæ¡å¼µããããã«å©ç¨ããã¾ããæ°ããç¬èªè¦ç´ ãå®ç¾©ããã«ã¯ãcomponentã使ç¨ãã¾ãã
ããã§ã¯ä¾ãããã¤ãè¦ã¦ã¿ã¾ãããã
directive
vs-match directiveã¯inputè¦ç´ ã«é©ç¨ã§ãã¾ããè¦ç´ ã®å¤æ´ãç£è¦ãã¦ãå¤ãç¹å®ã®ãã¿ã¼ã³ã«ä¸è´ããæã«directiveãè¦ç´ ã«matchã¯ã©ã¹ã追å ãã¾ãã
ããã¯æ¬¡ã®ããã«ä½¿ç¨ãã¾ã:
<input type="text" vs-match="^\d\d$">
説æããdirectiveã®é常ã«ç°¡åãªAngularJSã§ã®å®è£ ã¯æ¬¡ã®ããã«ãªãã¾ã:
//JS: directive("vsMatch", function(){ return { restrict: 'A', scope: {pattern: '@vsMatch'}, link: function(scope, element){ var exp = new RegExp(scope.pattern); element.on("keyup", function(){ exp.test(element.val()) ? element.addClass('match') : element.removeClass('match'); }); } }; });
ã§ã¯ãããAngularDartçã¨æ¯è¼ãã¦ã¿ã¾ãããã
//DART: @NgDirective(selector: '[vs-match]') class Match implements NgAttachAware{ @NgAttr("vs-match") String pattern; Element el; Match(this.el); attach(){ final exp = new RegExp(pattern); el.onKeyUp.listen((_) => exp.hasMatch(el.value) ? el.classes.add("match") : el.classes.remove("match")); } }
é ã«èª¬æãã¾ã:
- NgDirectiveã¯Angularã«ãã®ã¯ã©ã¹ãdirectiveã§ããã¨ä¼ãã¾ãã
- selectorå±æ§ã¯ãã®directiveããã¤æ´»æ§åãããããå®ç¾©ãã¾ããä»åã®ä¾ã§ã¯ãããã¯è¦ç´ ãvs-matchå±æ§ãæã¤æã§ãã
- ä»»æã®serviceãdirectiveã«æ³¨å ¥ã§ããã ãã§ãªããdirectiveãé©ç¨ãããè¦ç´ ãæ³¨å ¥ã§ãã¾ãããããMatch(this.el)ã®è¡ã£ã¦ãããã¨ã§ãã
- bindingã¯AngularJSã¨åæ§ã«ãããã渡ããã¨ã§è¨å®ã§ãã¾ãããããããã¯ã¢ããã¼ã·ã§ã³ã使ç¨ãã¦ãå®ç¾ã§ããå人çã«ã¯ãã¡ãã®æ¹ããã£ã¨èªã¿ãããç解ãããããã¨æãã¾ãã
- directiveã®ã³ã³ã¹ãã©ã¯ã¿ãå®è¡ãããæã«ã¯ããã¿ã¼ã³ã®å¤ã¯ã¾ã è¨å®ããã¦ãã¾ãããããã¯NgAttachAwareã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ãããã¨ã§è§£æ±ºãã¾ããNgAttachAwareã¤ã³ã¿ã¼ãã§ã¼ã¹ã«ã¯attachã¡ã½ããã宣è¨ããã¦ãã¦ãæ°ããdigestãçºçããæã«å®è¡ããã¾ãããã®æç¹ã§ã¯ãã¹ã¦ã®å±æ§ã®ãããã³ã°ãå®äºãã¦ããã®ã§ãå®å ¨ã«æ£è¦è¡¨ç¾ãæ§ç¯ã§ãã¾ãã
- æçµçã«ããªã³ã¯ãããé¢æ°ãã³ã³ãã¤ã«ãããé¢æ°ãããã¾ããã
component
次ã«è¦ã¦ãããã¨ã«ãªãcomponentã¯ãã³ã³ãã³ãã®è¡¨ç¤ºé表示ãåãæ¿ãã¾ããããã¯æ¬¡ã®ããã«ãã¦ä½¿ç¨ãã¾ã:
<toggle button="Toggle"> <p>Inside</p> </toggle>
ãã®componentã®AngularJSã§ã®å®è£ ã¯æ¬¡ã®ããã«ãªãã¾ã:
//JS: directive("toggle", function(){ return { restrict: 'E', replace: true, transclude: true, scope: {button: '@'}, template: "<div><button ng-click='toggle()'>{{button}}</button><div ng-transclude ng-if='showContent'/></div>", controller: function($scope){ $scope.showContent = false; $scope.toggle = function(){ $scope.showContent = !$scope.showContent ; }; } } })
ã§ã¯ãããDartçã¨æ¯è¼ãã¦ã¿ã¾ããã:
//DART: @NgComponent( selector: "toggle", publishAs: 't', template: "<button ng-click='t.toggle()'>{{t.button}}</button><content ng-if='t.showContent'/>" ) class Toggle { @NgAttr("button") String button; bool showContent = false; toggle() => showContent = !showContent; }
- NgComponentã¯Angularã«ãã®ã¯ã©ã¹ãcomponentã§ãããã¨ãä¼ãã¾ãã
- publishAsã¯ãã³ãã¬ã¼ãã®ä¸ã§ãã°ã«ãããªãã¸ã§ã¯ãã«ã¢ã¯ã»ã¹ããããã«å©ç¨ã§ããååã§ãããã®ååã¯æ¿å ¥ãããã³ã³ãã³ãã§ã¯ãªããã³ã³ãã¼ãã³ãã®ãã³ãã¬ã¼ãå ã§ã ãå©ç¨ã§ããã¨ãããã¨ã«æ³¨æãã¦ãã ããã
- templateã¯å½ç¶ãã®ç¬èªè¦ç´ ãã©ã®ããã«æç»ããããå®ç¾©ãã¾ãã
JSçã¨Dartçã¯åãããã«è¦ãã¾ããããã®å å´ã«ã¯é大ãªéããããã¾ãã
AngularDartã®componentã¯ãã³ãã¬ã¼ããæç»ããããã«shadow DOMã使ç¨ãã¾ãã
AngularJS:
AngularDart:
Shadow DOMã使ç¨ããã¨DOMã¨CSSãã«ãã»ã«åã§ããåå©ç¨å¯è½ãªã³ã³ãã¼ãã³ããæ§ç¯ããã®ã«é常ã«å½¹ç«ã¡ã¾ããAPIã«ã¤ãã¦ãWeb componentsã®ä»æ§ã«æ²¿ãããå¤æ´ãå ãããã¦ãã¾ãï¼ä¾ãã°ng-transcludeã¯contentã¨ç½®ãæãããã¾ããï¼ã
AngularDartã®componentã¯ãã®ãã³ãã¬ã¼ããä¿æããããã«templateè¦ç´ ã使ç¨ãã¾ãã
ããã«ããng-srcã®ãããªããã¯ãä¸è¦ã«ãªãã¾ããã
ã¾ã¨ããã¨ãdirectiveã¯DOMè¦ç´ ãæ¡å¼µããããã«ä½¿ç¨ããã¾ããcomponentã¯Web Componentsã®è»½éçã§ãç¬èªè¦ç´ ãä½æããããã«å©ç¨ããã¾ãã
controller
以ä¸ã®ä¾ã¯AngularJSã§å®è£ ãããé常ã«åç´ãªcontrollerã§ãã
//JS: <div ng-controller="CompareCtrl as ctrl"> First <input type="text" ng-model="ctrl.firstValue"> Second <input type="text" ng-model="ctrl.secondValue"> {{ctrl.valuesAreEqual()}} </div>
controller("CompareCtrl", function(){ this.firstValue = ""; this.secondValue = ""; this.valuesAreEqual = function(){ return this.firstValue == this.secondValue; }; });
Dartçã¯ããã¨å¤§ããç°ãªãã¾ãã
//DART: <div compare-ctrl> First <input type="text" ng-model="ctrl.firstValue"> Second <input type="text" ng-model="ctrl.secondValue"> {{ctrl.valuesAreEqual}} </div>
@NgController( selector: "[compare-ctrl]", publishAs: 'ctrl' ) class CompareCtlr { String firstValue = ""; String secondValue = ""; get valuesAreEqual => firstValue == secondValue; }
å
ã«è¿°ã¹ãããã«ãcontollerã¯åºæ¬çã«ã¯directiveã§ãè¦ç´ ä¸ã«æ°ããã¹ã³ã¼ããä½æãã¾ããæ°ããdirectiveãå®ç¾©ããã¨ãã«å©ç¨ã§ãããªãã·ã§ã³ã¯å
¨ã¦controllerãå®ç¾©ããã¨ãã«ãå©ç¨ã§ãã¾ããã¨ã¯ããããã¬ã¼ã ã¯ã¼ã¯ã«ãã£ã¦è¦å®ããã¦ããããã§ã¯ããã¾ããããcontrollerå
ã§ããããDOMæä½ãã¸ãã¯ãæ¸ããã¨ãé¿ããã®ã¯ããèãã§ãã
filter
æå¾ã«ãã©ã®ããã«ãã¦filterãå®ç¾©ã§ãããè¦ã¦è¡ãã¾ãããã
//JS: filter("isBlank", function(){ return function(value){ return value.length == 0; }; });
Dartçã§ã¯:
//DART: @NgFilter(name: 'isBlank') class IsBlank { call(value) => value.isEmpty; }
zoneã¨$scope.$apply
çç·´ããAngularéçºè ã¯æ¬¡ã®æ©è½ããããããæãã§ãããããã®è¨äºã§ç´¹ä»ããå 容ã®ä¸ã§ãæãè¡æçã ã¨æãã人ãããããããã¾ãã:
ãµã¼ããã¼ãã£è£½ã®componentã¨çµåããã¨ãã«$scope.$applyãå¼ã¶å¿ è¦ã¯ããã¾ããã
次ã®ä¾ã使ç¨ãã¦ä¸è¨ã«ã¤ãã¦ã説æãã¾ãããã
<div ng-controller="CountCtrl as ctrl"> {{ctrl.count}} </div>
CountCtrlã¯åã«countå¤æ°ãã¤ã³ã¯ãªã¡ã³ãããã ãã®controllerã§ãã
//JS: controller("CountCtrl", function(){ var c = this; this.count = 1; setInterval(function(){ c.count ++; }, 1000); })
çç·´ããAngularJSéçºè
ã§ããã°ãã®ã³ã¼ãã¯å£ãã¦ããã¨ããã«æ°ã¥ãã§ãããããã®ã¾ã¾ã§ã¯Angularã¯ã³ã¼ã«ããã¯å
ã§countå¤æ°ãå¤æ´ããããã¨ãç¥ããã¨ãã§ãã¾ããããã®åé¡ãä¿®æ£ããã«ã¯ã次ã®ããã«$scope.$applyã§å²ã¾ãªããã°ããã¾ããã
//JS: controller("CountCtrl", function($scope){ var c = this; this.count = 1; setInterval(function(){ $scope.$apply(function(){ c.count ++; }); }, 1000); })
ããã¯AngularJSã®åççãªå¶éã§ããããªãèªèº«ãAngularã«å¤æ´ããã§ãã¯ããããæ示çã«ç¤ºããªããã°ããã¾ããããã¬ã¼ã ã¯ã¼ã¯ã¯futureã©ã¤ãã©ãªããã³ãã«ãããã$interval serviceãæä¾ããããã¦ãããã®ä½æ¥ãå¿
è¦ãªå ´æãæå°åãããã¨åªåãã¦ãã¾ããããããä»ã®futureã©ã¤ãã©ãªã使ãå§ãããããµã¼ããã¼ãã£è£½ã®éåæcomponentã¨çµåãå§ããã¨ããã¯ã$scope.$applyã使ããªããã°ããã¾ããã
ããã§ã¯ããããDartçã¨æ¯ã¹ã¦ã¿ã¾ãããã
//DART: <div count-ctrl> {{ctrl.count}} </div>
@NgController( selector: "[count-ctrl]", publishAs: 'ctrl' ) class CountCtrl { num count = 0; CountCtrl(){ new Timer.periodic(new Duration(seconds: 1), (_) => count++); } }
Dartçã«ã¯$applyã¯ããã¾ãããã¡ããã¨åä½ãã¾ããTimerã¯Angularã«ã¤ãã¦ã¯ä½ãç¥ãã¾ããããã°ãããã§ã¯ãªãã§ããï¼ãããã©ã®ããã«åä½ãã¦ãããç解ããããã«ã¯ãZoneã¨ããæ¦å¿µã«ã¤ãã¦å¦ã¶å¿
è¦ãããã¾ãã
Dartããã¥ã¡ã³ã: Zoneã¯åçãªç¯å²ã®éåæçã表ãã¦ãã¾ããéåæã³ã¼ã«ããã¯ã¯ããããã¥ã¼ã«å ¥ããããzoneã®ä¸ã§å®è¡ããã¾ããä¾ãã°ãfuture.thenã®ã³ã¼ã«ããã¯ã¯thenãå®è¡ãããã®ã¨åãzoneã§å®è¡ããã¾ãã
Zoneã¯ã¤ãã³ããã¼ã¹ã®ç°å¢ã§ã®ã¹ã¬ãããã¼ã«ã«å¤æ°ã®ãããªãã®ã ã¨èããããããããã¾ãããç°å¢ã¯å¸¸ã«current zoneãæã£ã¦ãã¾ããããã¦å ¨ã¦ã®éåææä½ã®å ¨ã¦ã®ã³ã¼ã«ããã¯ã¯current zoneã§åä½ãã¾ããããã«ãã£ã¦Angularã¯å¤æ´ããã§ãã¯ãã¹ãå ´æãåããã¾ãã
ããã«ããã®ã¡ã«ããºã ã使ããã¨ã§ããã¬ã¼ã ã¯ã¼ã¯ã¯ããªãã®ããã°ã©ã ã®å®è¡ã«ã¤ãã¦ã®æ å ±ãéããä¾ãã°ãé·ãã¹ã¿ãã¯ãã¬ã¼ã¹ãçæãããã¨ãã§ãã¾ããããããã°ä¾å¤ãçºçããã¨ãã«ãmultiple VM turnsã«æ¸¡ã£ã¦ã¹ã¿ãã¯ãã¬ã¼ã¹ãè¦ããã¨ãã§ãã¾ããè¨ãã¾ã§ããªããããã¯éçºç°å¢ãå¤§å¹ ã«æ¹åããã§ãããã
ã¾ã¨ã
- AngularDart APIã¯ã¯ã©ã¹ãã¼ã¹ã§ãã
- ãã®ãã¬ã¼ã ã¯ã¼ã¯ã¯ååã«ããæ³¨å ¥ã§ã¯ãªãåã«ããæ³¨å ¥ã使ç¨ãã¾ãã
- ãªãã¸ã§ã¯ãã®åã¯DIã·ã¹ãã ã«ã©ã®ããã«ç»é²ããããã¨ã¯ç¡é¢ä¿ã§ãã
- NgControllerãNgDirectiveã®ãããªã¢ããã¼ã·ã§ã³ãæ³¨å ¥å¯è½ãªãªãã¸ã§ã¯ããè¨å®ããããã«ä½¿ç¨ããã¾ãã
- directiveãfilterãcomponentãcontrollerãservicesãå ¨ã¦ãvalueãtypeãfactoryã使ç¨ãã¦ç»é²ã§ãã¾ãã
- directiveã¯DOMè¦ç´ ãæ¡å¼µããããã«ä½¿ç¨ããã¾ãã
- componentã¯Web Componentsã®è»½éçã§ãç¬èªè¦ç´ ãä½æããããã«ä½¿ç¨ããã¾ãã
- componentã¯ãã³ãã¬ã¼ããæç»ããããã«shadow DOMã使ç¨ãã¾ãã
- controllerã¯ãããé©ç¨ãããè¦ç´ ã§æ°ããã¹ã³ã¼ããä½æããdirectiveã§ãã
- scopeã¯Dartã®zoneã使ç¨ãã¦èªåçã«çµã¿è¾¼ã¾ãã¾ãã$scope.$applyã¯ä¸è¦ã«ãªãã¾ããã
ãªã«ãJSã«ç§»æ¤ãããã®ã§ããã
MiskoとIgorがDevoxxで話した内容ã«ããã¨ããããã®å¤æ´ã®å¤ããç¹ã«ä»¥ä¸ã®ãã®ãAngularJSã«ç§»æ¤ãããäºå®ã®ããã§ãã
- åã«åºã¥ããæ³¨å ¥
- ã¢ããã¼ã·ã§ã³ã使ç¨ãããªãã¸ã§ã¯ãã®å®ç¾©
- Shadow DOMã®å©ç¨
- Zone
ããæ·±ãå¦ã¶ã«ã¯
ãã®è¨äºãããªãã®å ¥éã®å½¹ã«ç«ã¤ãã¨ãæãã§ãã¾ããããæ·±ãç¥ããããã°ä»¥ä¸ããã§ãã¯ãã¦ãã ãã: