SlideShare a Scribd company logo
TypeScriptで書くAngularJS @ GDG神戸2014.8.23




Database
RESTfulAPI
Controller
HTML
JSON
TypeScriptで書くAngularJS @ GDG神戸2014.8.23
TypeScriptで書くAngularJS @ GDG神戸2014.8.23
http://www.typescriptlang.org/
TypeScriptで書くAngularJS @ GDG神戸2014.8.23
TypeScriptで書くAngularJS @ GDG神戸2014.8.23




TypeScriptで書くAngularJS @ GDG神戸2014.8.23




Class.prototype.method = function(){…

TypeScriptで書くAngularJS @ GDG神戸2014.8.23
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
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); TS
var foo = 'abc';

var foo: string = 'abc';










TypeScriptで書くAngularJS @ GDG神戸2014.8.23


TypeScriptで書くAngularJS @ GDG神戸2014.8.23
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;
};
};
<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;
};
};
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
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
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




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


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








http://definitelytyped.org/


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




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


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;
}
TypeScriptで書くAngularJS @ GDG神戸2014.8.23
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
<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
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]);
TypeScriptで書くAngularJS @ GDG神戸2014.8.23




{{c.$scope.firstName}}
TypeScriptで書くAngularJS @ GDG神戸2014.8.23
TypeScriptで書くAngularJS @ GDG神戸2014.8.23
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
angular.module('MyAngularJs')
.factory('MyProvider', ['otherProvider', function (otherProvider) {
return new MyProvider(otherProvider);
}]);
JS
angular.module('MyAngularJs')
.service('MyProvider', ['otherProvider', MyProvider]);
JS
angular.module('MyAngularJs')
.factory('MyProvider', ['otherProvider', function (otherProvider) {
return new MyProvider(otherProvider);
}]);
JS
angular.module('MyAngularJs')
.service('MyProvider', ['otherProvider', MyProvider]);
JS














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]);
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
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
TypeScriptで書くAngularJS @ GDG神戸2014.8.23
TypeScriptで書くAngularJS @ GDG神戸2014.8.23


$resource<T>();
↓
ng.resource.IResourceClass<T>型のインスタンス
.query();
↓
ng.resource.IResourceArray<T>型のインスタンス
.$promise
↓
ng.IPromise<ng.resource.IResourceArray<T>>型のインスタンス
!
嫌んなってきた
$resource<T>();
↓
ng.resource.IResourceClass<T>型のインスタンス
.query();
↓
ng.resource.IResourceArray<T>型のインスタンス
.$promise
↓
ng.IPromise<ng.resource.IResourceArray<T>>型のインスタンス
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>
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>
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()








TypeScriptで書くAngularJS @ GDG神戸2014.8.23
TypeScriptで書くAngularJS @ GDG神戸2014.8.23

More Related Content

PDF
モダンAngularJS @ GDG中国2014.12.6
PDF
Geb for Testing Your Grails Application GR8Conf India 2016
PDF
Security: The Great WordPress Lockdown - WordCamp Melbourne - February 2011
PDF
3. javascript bangla tutorials
KEY
Rebooting TEI Pointers
PDF
Speeding up Red Team engagements with carnivorall
PDF
WordPressでIoTをはじめよう
TXT
Html
モダンAngularJS @ GDG中国2014.12.6
Geb for Testing Your Grails Application GR8Conf India 2016
Security: The Great WordPress Lockdown - WordCamp Melbourne - February 2011
3. javascript bangla tutorials
Rebooting TEI Pointers
Speeding up Red Team engagements with carnivorall
WordPressでIoTをはじめよう
Html

What's hot (20)

PDF
Cultura organizacional y mejora educativa
TXT
Vidéo approche en immobilier
PDF
HTTP For the Good or the Bad - FSEC Edition
PPTX
Representing Material Culture Online: Historic Clothing in Omeka
PDF
Keep it simple web development stack
DOCX
Algoritma 5 november wiwik p.l
PDF
Elgriegoenfichas 1bachillerato-sin membrete-1 copia
PDF
Beware of C++17
PDF
お題でGroovyプログラミング: Part A
TXT
Ip lab
PPTX
Symfony 1, mi viejo amigo
PPTX
Twas the night before Malware...
PDF
Wso2 esb-rest-integration
PDF
RCEC Email 3.5.03
PDF
6.2. Hacking most popular websites
PDF
Dec 2090 honorarios sca
PDF
Postman On Steroids
PDF
Why Redux-Observable?
PDF
The report of JavaOne2011 about groovy
PDF
IMCI 2008 Edition by WHO
Cultura organizacional y mejora educativa
Vidéo approche en immobilier
HTTP For the Good or the Bad - FSEC Edition
Representing Material Culture Online: Historic Clothing in Omeka
Keep it simple web development stack
Algoritma 5 november wiwik p.l
Elgriegoenfichas 1bachillerato-sin membrete-1 copia
Beware of C++17
お題でGroovyプログラミング: Part A
Ip lab
Symfony 1, mi viejo amigo
Twas the night before Malware...
Wso2 esb-rest-integration
RCEC Email 3.5.03
6.2. Hacking most popular websites
Dec 2090 honorarios sca
Postman On Steroids
Why Redux-Observable?
The report of JavaOne2011 about groovy
IMCI 2008 Edition by WHO
Ad

Viewers also liked (20)

PDF
noteをAngularJSで構築した話
PPTX
Angular2実践入門
PDF
イベント駆動AngularJS / 今から書くAngular 2.0
PPTX
AngularJS2でつまづいたこと
PDF
Angular1&2
PDF
~新しい着回しと出会おう~ 『XZ(クローゼット)』 を支える技術 -Cordova編-
PDF
Python3でwebアプリ
PDF
Dockerを雑に紹介
PDF
モックアップ共有のススメ
PPTX
CordovaでAngularJSアプリ開発
PPTX
はじめてのSpring Boot
PPTX
Vue.js 2.0を試してみた
PDF
Spring bootでweb バリデート編
PDF
フロントエンドのツール Yeoman を勘違いしていた
PDF
Vue.js入門
PDF
ビルドサーバで使うDocker
PPTX
Onsen UIが目指すもの
PDF
GitBucketで社内OSSしませんか?
PDF
More Azure Websites! - JAZUGさっぽろ "きたあず" 第5回勉強会ライトニングトーク
PPTX
Reactive Programming
noteをAngularJSで構築した話
Angular2実践入門
イベント駆動AngularJS / 今から書くAngular 2.0
AngularJS2でつまづいたこと
Angular1&2
~新しい着回しと出会おう~ 『XZ(クローゼット)』 を支える技術 -Cordova編-
Python3でwebアプリ
Dockerを雑に紹介
モックアップ共有のススメ
CordovaでAngularJSアプリ開発
はじめてのSpring Boot
Vue.js 2.0を試してみた
Spring bootでweb バリデート編
フロントエンドのツール Yeoman を勘違いしていた
Vue.js入門
ビルドサーバで使うDocker
Onsen UIが目指すもの
GitBucketで社内OSSしませんか?
More Azure Websites! - JAZUGさっぽろ "きたあず" 第5回勉強会ライトニングトーク
Reactive Programming
Ad

Similar to TypeScriptで書くAngularJS @ GDG神戸2014.8.23 (20)

PPTX
Angular2 for Beginners
PDF
TypeScript Introduction
PPTX
Basics of AngularJS
PDF
運用Closure Compiler 打造高品質的JavaScript
PPT
AngularJS Mobile Warsaw 20-10-2014
PDF
Building Powerful Applications with AngularJS 2 and TypeScript - David Giard
PPT
Typescript - why it's awesome
PPTX
Angular2 and TypeScript
PDF
Angular 2 overview in 60 minutes
PDF
Introduction to AngularJS
PPTX
AngularJS
PPTX
AngularJS Directives
PPTX
Building a TV show with Angular, Bootstrap, and Web Services
PPTX
AngularJs-training
PPTX
TypeScript
PPTX
Angular Presentation
PPTX
Prototype and angularjs $scopes
PDF
Introduction to angular js july 6th 2014
DOCX
Angular js
PDF
Building Universal Applications with Angular 2
Angular2 for Beginners
TypeScript Introduction
Basics of AngularJS
運用Closure Compiler 打造高品質的JavaScript
AngularJS Mobile Warsaw 20-10-2014
Building Powerful Applications with AngularJS 2 and TypeScript - David Giard
Typescript - why it's awesome
Angular2 and TypeScript
Angular 2 overview in 60 minutes
Introduction to AngularJS
AngularJS
AngularJS Directives
Building a TV show with Angular, Bootstrap, and Web Services
AngularJs-training
TypeScript
Angular Presentation
Prototype and angularjs $scopes
Introduction to angular js july 6th 2014
Angular js
Building Universal Applications with Angular 2

Recently uploaded (20)

PDF
composite construction of structures.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
web development for engineering and engineering
PPT
Mechanical Engineering MATERIALS Selection
PPTX
additive manufacturing of ss316l using mig welding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
PPT on Performance Review to get promotions
PPT
Project quality management in manufacturing
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Geodesy 1.pptx...............................................
PPTX
Construction Project Organization Group 2.pptx
composite construction of structures.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
web development for engineering and engineering
Mechanical Engineering MATERIALS Selection
additive manufacturing of ss316l using mig welding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
573137875-Attendance-Management-System-original
bas. eng. economics group 4 presentation 1.pptx
UNIT 4 Total Quality Management .pptx
PPT on Performance Review to get promotions
Project quality management in manufacturing
Lesson 3_Tessellation.pptx finite Mathematics
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Geodesy 1.pptx...............................................
Construction Project Organization Group 2.pptx

TypeScriptで書くAngularJS @ GDG神戸2014.8.23