82

TypeScriptで書くAngularJS @ GDG神戸2014.8.23

Embed Size (px)

DESCRIPTION

2014.8.23にAngularJS勉強会 by GDG神戸で使用したスライドです。

Citation preview

Page 1: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 2: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 3: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

DatabaseRESTful API

ControllerHTML

JSON

Page 4: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 5: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 6: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

http://www.typescriptlang.org/

Page 7: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 8: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 9: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 10: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 11: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 12: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

Class.prototype.method = function(){…

Page 13: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 14: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 15: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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

Page 16: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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

Page 17: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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

Page 18: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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

Page 19: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

var foo = 'abc';var foo: string = 'abc';

Page 20: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 21: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 22: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 23: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 24: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 25: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

<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; }; };

Page 26: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

<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; }; };

Page 27: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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

Page 28: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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

Page 29: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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]);

Page 30: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

// ... 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

Page 31: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

class SampleCtrl { constructor ( public $scope ) { // ... } } !class SampleCtrl { public $scope; constructor ($scope) { this.$scope = $scope; // ... } }

TS

Page 32: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

/sample.ts(1,1): Cannot find name 'angular'.

Page 33: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 34: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

http://definitelytyped.org/

Page 35: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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]);

Page 36: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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]);

Page 37: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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]);

Page 38: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 39: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 40: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

TS

// ... !class SampleCtrl { constructor ( public $scope ) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } !// ...

Page 41: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

TS

// ... !class SampleCtrl { constructor ( public $scope ) { this.$scope.firstName = 'John'; this.$scope.lastName = 'Doe'; ! this.$scope.getFullName = () => { return this.$scope.firstName + ' ' + this.$scope.lastName; }; } } !// ...

!

ここが増えそうなのは 目に見えている…

Page 42: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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'; }

Page 43: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

TS

constructor ( public $scope ) { this.init(); } !private init() { // ... } !public getFullName(): string { return this.$scope.firstName + ' ' + this.$scope.lastName; }

Page 44: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

?!

Page 45: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

this.$scope.getFullName = () => { return // ... };

Page 46: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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; }

Page 47: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 48: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 49: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

<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

Page 50: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

<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

Page 51: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

<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

Page 52: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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]);

Page 53: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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]);

Page 54: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 55: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 56: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

{{c.$scope.firstName}}

Page 57: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 58: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 59: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 60: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 61: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

angular.module('MyAngularJs') .factory('MyProvider', ['otherProvider', function (otherProvider) { return new MyProvider(otherProvider); }]); JS

angular.module('MyAngularJs') .service('MyProvider', ['otherProvider', MyProvider]);

JS

Page 62: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

angular.module('MyAngularJs') .factory('MyProvider', ['otherProvider', function (otherProvider) { return new MyProvider(otherProvider); }]); JS

angular.module('MyAngularJs') .service('MyProvider', ['otherProvider', MyProvider]);

JS

Page 63: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

angular.module('MyAngularJs') .factory('MyProvider', ['otherProvider', function (otherProvider) { return new MyProvider(otherProvider); }]); JS

angular.module('MyAngularJs') .service('MyProvider', ['otherProvider', MyProvider]);

JS

Page 64: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 65: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 66: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 67: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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]);

Page 68: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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,

TS

Page 69: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

url: baseApi+'/:id', 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

Page 70: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 71: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 72: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 73: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

$resource<T>(); ↓

ng.resource.IResourceClass<T>型のインスタンス .query();

↓ ng.resource.IResourceArray<T>型のインスタンス

.$promise ↓

ng.IPromise<ng.resource.IResourceArray<T>>型のインスタンス

Page 74: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

!嫌んなってきた

$resource<T>(); ↓

ng.resource.IResourceClass<T>型のインスタンス .query();

↓ ng.resource.IResourceArray<T>型のインスタンス

.$promise ↓

ng.IPromise<ng.resource.IResourceArray<T>>型のインスタンス

Page 75: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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>

Page 76: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

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>

Page 77: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 78: TypeScriptで書くAngularJS @ GDG神戸2014.8.23

ng.IPromise<ng.resource.IResourceArray<T>>(promiseValue: T)

"ng.resource.IResourceArray<T>"

!

"ng.resource.IResourceArray<T>"

$resource<T>() IResourceArray<T>

!

$resource<T>()

.then()

Page 79: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 80: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 81: TypeScriptで書くAngularJS @ GDG神戸2014.8.23
Page 82: TypeScriptで書くAngularJS @ GDG神戸2014.8.23