- {{it}}
Pourquoi "tout" changer ?
Par rapport à AngularJS
Encore un nouveau langage à apprendre ?
@CanSpeak('Hello world!')
class Person {
private name: string;
constructor(name: string) {
this.name = name;
}
introduceYourself(): void {
console.log('My name is ' + this.name);
}
}
let p: Person = new Person('Kevin');
p.introduceYourself(); // My name is Kevin
(<any> p).speak(); // Hello world!
var Person = (function () {
function Person(name) {
this.name = name;
}
Person.prototype.introduceYourself = function () {
console.log('My name is ' + this.name);
};
return Person;
}());
var p = new Person('Kevin');
p.introduceYourself(); // My name is Kevin
Qui a compris ?
class Person {
constructor(name) {
this.name = name;
}
introduceYourself() {
console.log('My name is ' + this.name);
}
}
let p = new Person('Kevin');
p.introduceYourself(); // My name is Kevin
Heu, c'est pareil non ?
Est-ce une contrainte ?
any
let p1: any = new Person('Kevin');
let p2 = new Person('Kevin');
Modification du prototype
des classes
@CanSpeak('Hello world!')
class Person {}
// p.speak();
function CanSpeak(message: string) {
return function(target: Function) {
target.prototype.speak = () => console.log(message);
}
}
CanSpeak('Hello world!')(Person);
ECMAScript 2016 : décorateur
Ok, TypeScript c'est cool !
Observable
La programmation réactive est un paradigme de programmation visant à conserver une cohérence d'ensemble en propageant les modifications
d'une source réactive (modification d'une variable, entrée utilisateur, etc.) aux éléments dépendants de cette source.
Wikipedia
Natif : ES5
let promise = new Promise(
(resolve, reject) => {
// ...
resolve('Success...');
//reject('Fail...');
}
);
promise.then( // Ok
(val) => console.log(val)
).catch( // Error
(err) => console.error(err)
);
Bibliothèque externe
import {EventEmitter} from '@angular/core';
import {Observable} from "rxjs";
let observable: EventEmitter<string> = new EventEmitter<string>();
observable.subscribe(
(val: string) => console.log(val),
(err: any) => console.error(err)
);
observable.emit('Success...');
//observable.error('Fail...');
Promise
Service vs Factory vs Provider
angular.module('foo')
.service('myService', ['otherService', function(otherService) {
this.doSomething = function (arg) { /*...*/ }
}]);
angular.module('foo')
.factory('myService', ['otherService', function(otherService) {
return {
doSomething: function (arg) { /*...*/ }
};
}]);
angular.module('foo')
.provider('myService', ['otherService', function(otherService) {
this.$get = function () {
return ...
}
}]);
Qui a compris ?
POO = classe + méthodes (+ attributs)
import { Injectable } from '@angular/core';
import { OtherService } from '...';
@Injectable()
class MyService {
constructor(otherService: OtherService) {
// configuration
}
doSomething(arg) {
// ...
}
}
Transformation de données
{{'toto' | upper}}
Nommé "filtre"
angular.module('foo', [])
.filter('upper', function() {
return function(value, arg1, arg2) {
return value.toUpperCase();
};
});
interface PipeTransform {
transform(value: any, ...args: any[]): any;
}
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'upper' })
class UpperPipe implements PipeTransform {
transform(value: string): string {
return value.toUpperCase();
}
}
I'm flashing...
angular.module('foo', [])
.directive('flashing', function() {
return {
restrict: 'A',
link: function(scope, element, attributes) {
var i = 0;
var initialColor = this.element.style.backgroundColor;
window.setInterval(
function() {
var color = (i%2==0 ? 'yellow' : 'none');
element.css({ backgroundColor: color });
i = i+1;
}, 1000
);
}
};
});
import { Directive, ElementRef } from '@angular/core';
@Directive({ selector: '[flashing]' })
export class FlashingDirective {
constructor(element: ElementRef) {
let i: number = 0;
let initialColor: string = element.nativeElement.style.backgroundColor;
window.setInterval(
() => {
let color: string = (i%2==0 ? 'yellow' : initialColor);
element.nativeElement.style.backgroundColor = color;
i = i+1;
}, 1000
);
}
}
import { Directive, ElementRef } from '@angular/core';
@Directive({ selector: '[flashing]' })
export class FlashingDirective {
private i: number = 0;
private initialColor: string;
constructor(private element: ElementRef) {
this.initialColor = this.element.nativeElement.style.backgroundColor;
window.setInterval(
() => {
this.changeColor();
i = i+1;
}, 1000
);
}
private changeColor(): void {
let color: string = (i%2==0 ? 'yellow' : this.initialColor);
this.element.nativeElement.style.backgroundColor = color;
}
}
import { Component, Input, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'hello',
template: 'Hello {{name}}!
'
})
class HelloComponent {
@Input() name: string;
@Output() touch: EventEmitter<string> = new EventEmitter<string>();
onClick(): void {
this.touch.emit("Click me I'm famous!");
}
}
class
: contrôleurtemplate
: vue
{{expression}}
<comp [attr]="expression">
<comp (attr)="handler">
<comp [(attr)]="property">
@Component({
selector: 'my-app',
template: ` `
})
class AppComponent {
myProp: string = 'Pinguet62';
touched(message: string): void {
console.log(message);
}
}
(function(app) {
app.AppComponent = ng.core
.Component({
selector: 'hello',
template: 'Hello world!'
})
.Class({
constructor: function() {}
});
})(window.app || (window.app = {}));
Pattern composite
@Component({
selector: 'my-app',
template: `
`
})
class AppComponent {}
@Component({
selector: 'my-content',
template: `
`
})
class ContentComponent {}
[
{ id: 1, name: 'Golf GTI', brand: 'Volkswagen' },
{ id: 2, name: 'Décapotable rouge', brand: 'BMW' },
{ id: 3, name: '208', brand: 'Peugeot' }
]