Share
Embed
TypeScriptで書くAngularJS @ GDG神戸2014.8.23
2014年8月23日発表分の資料をSlideShareより再掲。
http://www.slideshare.net/armorik83/gdgkobe-angularjs-2
OKUNOKENTARO
August 23, 2014
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
82 slides
-
˖ ؙٓأك٦أזؔـآؙؑز䭷ぢ鎉铂 ˖ Class.prototype.method = function(){… ⦜זQSPUPUZQF剅ֻ䗳銲כ֮תׇ ˖ ٗآحؙכⱄⵃ欽ךؙٓأחתה ˖
5ZQPכ؝ٝػ؎ָٓⰋ⸂ד䭷䶯 ˖ ؝ٝػ؎ٕؒٓ٦ָ⳿וֶַַֿ׃ְ ז5ZQF4DSJQU -
class Greeter { private greeting: string; constructor(message: string) { this.greeting
= message; } public greet() { return 'Hello, ' + this.greeting; } } ! var greeter = new Greeter('world'); ! var button = document.createElement('button'); button.textContent = 'Say Hello'; button.onclick = () => { alert(greeter.greet()); }; ! document.body.appendChild(button); TS -
var Greeter = (function () { function Greeter(message) { this.greeting
= message; } Greeter.prototype.greet = function () { return 'Hello, ' + this.greeting; }; return Greeter; })(); ! var greeter = new Greeter('world'); ! var button = document.createElement('button'); button.textContent = 'Say Hello'; button.onclick = function () { alert(greeter.greet()); }; document.body.appendChild(button); //# sourceMappingURL=greeter.js.map JS -
class Greeter { private greeting: string; constructor(message: string) { this.greeting
= message; } public greet() { return 'Hello, ' + this.greeting; } } ! var greeter = new Greeter('world'); ! var button = document.createElement('button'); button.textContent = 'Say Hello'; button.onclick = () => { alert(greeter.greet()); }; ! document.body.appendChild(button); TS -
class Greeter { private greeting: string; constructor(message: string) { this.greeting
= message; } public greet() { return 'Hello, ' + this.greeting; } } ! var greeter = new Greeter('world'); ! var button = document.createElement('button'); button.textContent = 'Say Hello'; button.onclick = () => { alert(greeter.greet()); }; ! document.body.appendChild(button); ̒䒷侧ך㘗 ،ؙإأ⥜귅㶨̓ OFX דㄎל̓ ֿך鴟כ֮ת㢌זְ ̓،ٗ٦ꟼ侧䒭&4⯓《 TS -
ך➭ך暴䗙 ˖ 㘗䱿锷֮תׅ ˖ var foo = 'abc'; var foo:
string = 'abc';ה剅ַזֻגְְ ˖ JOUFSGBDFד㹋鄲ךזְ㘗ךך㹀纏 ˖ NPEVMFדせ瑞涸ז⢪ְ倯 ֿֿדְֲNPEVMFכ$PNNPO+4הכ殯ז ˖ آؑطؙٔأծ竰䪫ծזוזו -
˖ ַתׁמ孑 ջ5ZQF4DSJQUٔؿ؋ٖٝأ խ7FS㼎䘔ռ ˖ 俑岀ֽדזֻծ涪橆㞮ך圓眠ַ 佄䴂خ٦ٕHSVOU CPXFS UTEזוך 鍑铡䲓鯹
˖ +BWB4DSJQU㛇燉濼陎鯹גתׅ ֶׅׅ剅硂 !WWBLBNF -
<body ng-app> <div ng-controller="SampleCtrl"> <strong>First name:</strong> {{firstName}}<br> </div> </body> HTML
JS function SampleCtrl ($scope) { $scope.firstName = 'John'; $scope.lastName = 'Doe'; ! $scope.getFullName = function () { return $scope.firstName + ' ' + $scope.lastName; }; }; -
<body ng-app> <div ng-controller="sampleCtrl"> <strong>First name:</strong> {{firstName}}<br> </div> </body> HTML
JS ! 5ZQF4DSJQUכ♶銲הְֲ倯 ֿך剅ֹ倯כװת׃׳ֲ ֮ה֮הخְٓדׅ ̕ function SampleCtrl ($scope) { $scope.firstName = 'John'; $scope.lastName = 'Doe'; ! $scope.getFullName = function () { return $scope.firstName + ' ' + $scope.lastName; }; }; -
angular.module('MyAngularJs', [/*...*/]); // Setter ! function SampleCtrl ($scope) { $scope.firstName
= 'John'; $scope.lastName = 'Doe'; ! $scope.getFullName = function () { return $scope.firstName + ' ' + $scope.lastName; }; }; ! angular.module('MyAngularJs') // Getter .controller('SampleCtrl', [ '$scope', // 使用するServiceは全て文字列で表記 SampleCtrl ]); JS BOHVMBSNPEVMF ⢪ְת׃׳ֲ -
angular.module('MyAngularJs', [/*...*/]); // Setter ! function SampleCtrl ($scope) { $scope.firstName
= 'John'; $scope.lastName = 'Doe'; ! $scope.getFullName = function () { return $scope.firstName + ' ' + $scope.lastName; }; }; ! angular.module('MyAngularJs') // Getter .controller('SampleCtrl', [ '$scope', // 使用するServiceは全て文字列で表記 SampleCtrl ]); JS ֿֿ̓5ZQF4DSJQUח׃תׅ -
TS // ... class SampleCtrl { constructor ( public $scope
) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', ['$scope', SampleCtrl]); -
// ... class SampleCtrl { constructor ( public $scope )
{ this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', ['$scope', SampleCtrl]); TS ̒׳ה孡䭯䝤ְַ׃זְ խֽוծ"OHVMBS+4ך䒷侧כ㟓ִָ խזךד何遤׃גֶֻ ̓،ٗ٦ꟼ侧זךדUIJTָ䭷ׅⰻ㺁כ խ㢌זְ@UIJTהַ׃זֻגְְ ̕DMBTT㹑鎉׃תׅ DMBTTך؝ٝأزؙٓةָ床̓ -
class SampleCtrl { constructor ( public $scope ) { //
... } } ! class SampleCtrl { public $scope; constructor ($scope) { this.$scope = $scope; // ... } } TS ずֿׄה ׃גתׅ ♳ךקֲָءٝفٕ -
؝ٝػ؎ٕ ˖ ➙剅ְ5ZQF4DSJQU؝ٝػ؎ٕׅה ˖ ؒٓ٦ /sample.ts(1,1): Cannot find name 'angular'.
˖ BOHVMBSהְֲ㢌侧כ搀ְה鎉תׅ -
%FOJUFMZ5ZQFE ˖ %FOJUFMZ5ZQFEהְֲ؝ىُصذ؍דכծ +4ٓ؎ـٓٔך剣せוֿח㼎䘔׃ 㘗㹀纏ؿ؋؎ٕ鞢㺡ח⥂㸚׃גְ http://definitelytyped.org/ ˖ K2VFSZ %KT 6OEFSTDPSFKT
+BTNJOF .POHP%#זוזו˘ ˖ "OHVMBS+4֮ -
TS /// <reference path="angularjs/angular.d.ts" /> ! class SampleCtrl { constructor
( public $scope ) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', ['$scope', SampleCtrl]); ̓撑㹑鎉ׅה ̕؝ٝػ؎ָٓ㢌侧BOHVMBS钠陎㘗➰ֹד -
TS declare var angular: any; ! class SampleCtrl { constructor
( public $scope ) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', ['$scope', SampleCtrl]); ̓EFDMBSFהְֲչ،ٝؽؒٝز㹑鎉պ欽ְה խ㘗㹀纏ؿ؋؎ָٕ搀ֻג؝ٝػ؎ٓח侄ִ խ؝ٝػ؎ٕ穠卓חכ䕦갟׃זְ -
TS declare var angular: any; ! class SampleCtrl { constructor
( public $scope ) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', ['$scope', SampleCtrl]); -
BOZ㘗 ˖ 5ZQF4DSJQUחכOVNCFS TUSJOH CPPMFBOה ְֲفٔىذ؍ـ㘗ָ欽䠐ׁגְ ˖ ך➭ծWPJE OVMM VOEFOFE㘗欽䠐ׁגְ
ֿך㘗דך㢌侧㹑鎉כדֹזְ ˖ BOZ㘗כ䖞勻ך+BWB4DSJQUך㘗 ˖ ؝ٝػ؎ٕؒٓ٦װ䖤׆럀ׇ㜥さװ 㹀纏ؿ؋؎ٕ锃麦דֹזְ㜥さזוח -
TS // ... ! class SampleCtrl { constructor ( public
$scope ) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! // ... -
TS // ... ! class SampleCtrl { constructor ( public
$scope ) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } ! // ... ! ͕͜͜૿͑ͦ͏ͳͷ ʹݟ͍͑ͯΔʜ -
TS constructor ( public $scope ) { this.init(); ! this.$scope.getFullName
= () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } ! private init() { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; } -
TS constructor ( public $scope ) { this.init(); } !
private init() { // ... } ! public getFullName(): string { return this.$scope.firstName + ' ' + this.$scope.lastName; } ̕䨱⦼ך㘗 -
غ؎ٝس怩ח岣䠐 ˖ TDPQFח秡➰ֽגְזְًاحسכ ⹛⡲׃תׇ this.$scope.getFullName = () => { return
// ... }; 何㢌כׯהغ؎ٝسׁגְ -
TS constructor ( public $scope ) { this.init(); this.$scope.getFullName =
this.getFullName.bind(this); } ! private init() { // ... } ! public getFullName(): string { return this.$scope.firstName + ' ' + this.$scope.lastName; } 姻׃ֻכCJOE ⢪ְתׅ -
<body ng-app> <div ng-controller="SampleCtrl"> <strong>First name:</strong> {{firstName}}<br> <strong>Last name:</strong> {{lastName}}<br>
<strong>Full name:</strong> {{getFullName()}}<br> </div> </body> HTML -
<body ng-app> <div ng-controller="SampleCtrl as c"> <strong>First name:</strong> {{c.firstName}}<br> <strong>Last
name:</strong> {{c.lastName}}<br> <strong>Full name:</strong> {{c.getFullName()}}<br> </div> </body> HTML -
<body ng-app> <div ng-controller="SampleCtrl as c"> <strong>First name:</strong> {{c.firstName}}<br> <strong>Last
name:</strong> {{c.lastName}}<br> <strong>Full name:</strong> {{c.getFullName()}}<br> </div> </body> HTML ̕呓秛ׅ㢌侧せ ̓㢌侧せفٗػذ؍ խ➰ֽזְהSPPU4DPQF䪔ְ -
TS class SampleCtrl { public firstName: string; public lastName: string;
! constructor () { this.init(); } ! private init() { this.firstName = 'John'; this.lastName = 'Doe'; } ! public getFullName(): string { return this.firstName + ' ' + this.lastName; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', [SampleCtrl]); -
TS class SampleCtrl { public firstName: string; public lastName: string;
! constructor () { this.init(); } ! private init() { this.firstName = 'John'; this.lastName = 'Doe'; } ! public getFullName(): string { return this.firstName + ' ' + this.lastName; } } ! angular.module('MyAngularJs') .controller('SampleCtrl', [SampleCtrl]); ̕TDPQF♶銲 ̒TDPQF♶銲 ̒UIJTך湫䱸ךفٗػذ؍ח խזךד㘗㹀纏 -
'BDUPSZ angular.module('MyAngularJs') .factory('MyProvider', ['otherProvider', function (otherProvider) { return new MyProvider(otherProvider);
}]); JS angular.module('MyAngularJs') .service('MyProvider', ['otherProvider', MyProvider]); 4FSWJDF JS -
'BDUPSZ angular.module('MyAngularJs') .factory('MyProvider', ['otherProvider', function (otherProvider) { return new MyProvider(otherProvider);
}]); JS angular.module('MyAngularJs') .service('MyProvider', ['otherProvider', MyProvider]); 4FSWJDF JS ̓؎ٝأةٝأ欰䧭׃ג鵤ׅ ؝ٝأزؙٓة♷ִג"OHVMBS+4ָ欰䧭ׅ̓ 4JOHMFUPOדׅխ խ -
'BDUPSZ angular.module('MyAngularJs') .factory('MyProvider', ['otherProvider', function (otherProvider) { return new MyProvider(otherProvider);
}]); JS angular.module('MyAngularJs') .service('MyProvider', ['otherProvider', MyProvider]); 4FSWJDF JS 5ZQF4DSJQUד 䪔ְװְׅךכ4FSWJDF -
⢪ְⴓֽ ˖ 4JOHMFUPOַծꟼ侧תה0CKFDUַהְֲ麩ְ ˖ 朐䡾⥂㶷כ4FSWJDFח䬐ׇ׆SPPU4DPQFחׇׁץֹ ˖ 'BDUPSZכ0CKFDU鵤ׅךדծًٝغךⰕך嚊䙀ח 鵚ְךꥷכծBOHVMBSCJOE ׅ ˖
⢪ְװְׅ倯דְְה䙼ג 5ZQF4DSJQUד剅ֻז4FSWJDFַ -
TS class MyResource { constructor( private $q: ng.IQService, private $resource:
ng.resource.IResourceService ) { // ... } ! private resource(): IResourceClass { var baseApi = '/api/entities'; var params: any = null; var actions = { getWithEntityId: { method: 'GET', url: baseApi+'/:id', isArray: true} }; return <IResourceClass>this.$resource(baseApi, params, actions); } ! public fetchEntity(id: my.obj.EntityId): IPromiseReturnEntity { var dfd = this.deferAcceptEntity(); ! var query = {id: id}; this.resource().getWithEntityId( query, this.success(), this.failure() ).$promise.then(this.fetchThen(dfd)); ! return dfd.promise; } private fetchThen(dfd: IDeferredAcceptEntity): (res: my.obj.IEntities) => void { return (res) => { var e: my.obj.IEntity = res[0]; dfd.resolve(e); }; } } ! angular.module('MyAngularJs') .service('MyResource', ['$q', '$resource', MyResource]); -
class MyResource { constructor( private $q: ng.IQService, private $resource: ng.resource.IResourceService
) { // ... } ! private resource(): IResourceClass { var baseApi = '/api/entities'; var params: any = null; var actions = { getWithEntityId: { method: 'GET', url: baseApi+'/:id', isArray: true} }; return <IResourceClass>this.$resource(baseApi, params, actions); } ! public fetchEntity(id: my.obj.EntityId): IPromiseReturnEntity { var dfd = this.deferAcceptEntity(); ! var query = {id: id}; this.resource().getWithEntityId( TS ̕BOHVMBSEUTך㘗㹀纏ؿ؋؎ٕ崞欽 ٍ̓ؗأز׃זְה鋵ך㘗ה׃גؒٓ٦⳿ׅ ̕ⴽךؿ؋؎ٕד杝荈ח*3FTPVSDF$MBTT㹀纏 խBOHVMBSEUTFYUFOET竰䪫ׇׁגְ -
isArray: true} }; return <IResourceClass>this.$resource(baseApi, params, actions); } ! public
fetchEntity(id: number): IPromiseReturnEntity { var dfd = this.deferAcceptEntity(); ! var query = {id: id}; this.resource().getWithEntityId( query, this.success(), this.failure() ).$promise.then(this.fetchThen(dfd)); ! return dfd.promise; } private fetchThen(dfd: IDeferredAcceptEntity): (res: my.obj.IEntities) => void { return (res) => { var e: my.obj.IEntity = res[0]; dfd.resolve(e); }; } } ! angular.module('MyAngularJs') .service('MyResource', ['$q', '$resource', MyResource]); TS ֿֿתדָⰋג䨱⦼ך㘗 ̕杝荈ך㘗 ̓杝荈ך㘗 ̕%*ׅ1SPWJEFSה荈魦ך؝ٝأزؙٓة խ䧭⸆儗ה㣟侁儗חㄎלꟼ侧 ̒➙㔐כꟼ⤘זְ -
BOHVMBSEUT余殛 ! ݏΜͳ͖ͬͯͨ $resource<T>(); ↓ ng.resource.IResourceClass<T>型のインスタンス .query(); ↓ ng.resource.IResourceArray<T>型のインスタンス .$promise
↓ ng.IPromise<ng.resource.IResourceArray<T>>型のインスタンス -
BOHVMBSEUT ng.resource.IResourceClass<T>型のインスタンス .query(); ↓ ng.resource.IResourceArray<T>型のインスタンス .$promise ↓ ng.IPromise<ng.resource.IResourceArray<T>>型のインスタンス .then<TResult>( successCallback:
(promiseValue: T) => TResult, errorCallback?: (reason: any) => TResult, notifyCallback?: (state: any) => any ): IPromise<TResult> -
BOHVMBSEUT ng.resource.IResourceClass<T>型のインスタンス .query(); ↓ ng.resource.IResourceArray<T>型のインスタンス .$promise ↓ ng.IPromise<ng.resource.IResourceArray<T>>型のインスタンス .then<TResult>( successCallback:
(promiseValue: T) => TResult, errorCallback?: (reason: any) => TResult, notifyCallback?: (state: any) => any ): IPromise<TResult> 殅䠐ׅץֹזךכծֿך鴟כ Ⰻ鿇דչמהאך㘗せպד֮הְֲֿה չ5䒷侧חה53FTVMU鵤ׅꟼ侧պ䒷侧הׅ -
BOHVMBSEUT余殛 ng.IPromise<ng.resource.IResourceArray<T>> ה5ָطأز׃גְךדծ(promiseValue: T)ך5כ "ng.resource.IResourceArray<T>"Ⰻ⡤䭷ׅկ ! "ng.resource.IResourceArray<T>"ך⚥ך5כ ♧殢剑ⴱծ$resource<T>()ה剅ְ儗ך5կIResourceArray<T>כ"1* ַ鷏גֻ穠卓ךꂁךֿהկ !
אתծךꂁך⦐ղך㘗$resource<T>()ך5ח䭷㹀׃גֶֽ ל.then()ך⚥ד㘗➰ֽׁ朐䡾ד⢪ִ