@EmmanuelDemey
Consultant & Formateur Web
Zenika Lille
@EmmanuelDemey
Web / Domotique / Biérologie
Préparer votre application à Angular2
angular.module('MyApp')
.service('BreweryService', function(){
this.getBeers(){ ... }
});
angular.module('MyApp')
.controller('BreweryCtrl',
['BreweryService',
function(BreweryService){
BreweryService.getBeers();
}]);
function BreweryCtrl(BreweryService){
BreweryService.getBeers();
}
BreweryCtrl.$inject = ['BreweryService']
angular.module('MyApp')
.controller('BreweryCtrl', BreweryCtrl);
angular.module('MyApp')
.controller('BreweryCtrl',
function(BreweryService){
BreweryService.getBeers();
});
angular.module('MyApp')
.controller('BreweryCtrl',
function(){
this.beers = [];
});
<ul ng-controller="BreweryCtrl as bc">
<li ng-repeat="beer in bc.beers">
{{beer}}
</li>
</ul>
class BreweryCtrl {
constructor() { ... }
selectBeer() { ... }
}
export { BreweryCtrl }
import { BreweryCtrl} from './BreweryCtrl';
angular
.module('app', [])
.controller('BreweryCtrl', BreweryCtrl);
Mise en pratique
Les choses que nous n'aimons pas...
DI (provider, service, factory...)
MV*
MV*
MV*
Filtres
app.directive('MyDirective', function(){
return {
restrict: 'AE',
require: '?^^ngModel',
scope: { variable: '@' },
compile: function(...) {
return {
pre: function(...) { ... },
post: function(...) { ... }
}
},
link: function(...) { ... }
}
});
<input ng-model="firstName">
<p>
{{firstName }}
</p>
La solution... Angular 2
<app></app>
menu
grid
gallery
DI
(classes ES6 ou TypeScript)
Pipes
(classes ES6 ou TypeScript)
ES5
ES2015
TypeScript
Les composants
import {Component} from '@angular/core';
@Component({
selector: 'my-app',
template: `<main>
<h1> This is my first Angular2 app</h1>
</main>`
})
class MyAppComponent {
}
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { platformBrowserDynamic }
from '@angular/platform-browser-dynamic';
import { MyAppComponent } from './app.component';
@NgModule({
declarations: [
MyAppComponent
],
imports: [
BrowserModule
],
bootstrap: [MyAppComponent]
})
export class AppModule { }
platformBrowserDynamic().bootstrapModule(AppModule);
Root Cpt
Child1 Cpt
Child2 Cpt
[property]="expression"
(event)="update()"
<input [property]="expression" />
<body>
<h1>My First Angular2 app</h1>
</body>
<body>
<h1 [textContent]="'My First Angular2 app'">
</h1>
</body>
<body>
<h1>{{'My First Angular2 app'}}
</h1>
</body>
//<beer-item [beer]="'Maredsous'"></beer-item>
@Component({
selector: 'beer-item',
template: `<section>
<h2>{{beer}}</h2>
<button>Je veux celle-ci !</button>
</section>`
})
export class BeerItem {
@Input() beer: string;
}
<input (event)="expression" />
//<beer-item [beer]="'Maredsous'" (selectBeer)="sendToBeerTap($event)"></beer-item>
@Component({
selector: 'beer-item',
template: `<section>
<h2>{{beer}}</h2>
<button (click)="selectItem()">Je veux celle-ci !</button>
</section>`
})
export class BeerItem {
@Input() beer: string;
@Output() selectBeer: EventEmitter = new EventEmitter<string>();
selectItem() {
this.selectBeer.next(this.beer);
}
}
Injection de Dépendance
@Component({
selector: 'my-app',
template: `<main><h1> Welcome to our {{breweryName}}</h1></main>`
})
class MyAppComponent {
breweryName:string;
constructor(private breweryService:BreweryService) {
this.breweryName = this.breweryService.getBreweryName();
}
}
//module.ts
@NgModule({
providers: [BreweryService]
})
export class AppModule { }
@Component({
selector: 'my-app',
template: `<main>
<h1> Welcome to our {{breweryName}}</h1>
</main>`
})
class MyAppComponent {
constructor(@Inject('title') public breweryName:string) { ... }
}
//module.ts
@NgModule({
providers: [{ provide: 'title', useValue: 'Zenika Brewery' }]
})
export class AppModule { }
@Component({
selector: 'my-app',
template: `<main>
<h1> Welcome to our {{breweryName}}</h1>
</main>`
})
class MyAppComponent {
constructor(public breweryService:BreweryService) { ... }
}
//module.ts
@NgModule({
providers: [{ provide: BreweryService, useClass: BBLBreweryService }]
})
export class AppModule { }
@Component({
selector: 'my-app',
template: `<main>
<welcome-message></welcome-message>
</main>`
})
class MyAppComponent {
constructor(@Inject('title') public breweryName:string) { ... }
}
//module.ts
@NgModule({
declarations: [
MyAppComponent, WelcomeMessage
],
providers: [[{ provide: 'title', useValue: 'Zenika Brewery' }]
})
export class AppModule { }
@Component({
selector: 'welcome-message',
template: `<h1>Welcome to our {{breweryName}}</h1>`
})
export class WelcomeMessage {
constructor(@Inject('title') public breweryName:string) { ... }
}
@Component({
selector: 'welcome-message',
template: `<h1>Welcome to our {{breweryName}}</h1>`,
providers: [
[{ provide: 'title', useValue: 'Awesome Zenika Brewery' }]
]
})
export class WelcomeMessage {
constructor(@Inject('title') public breweryName:string){ ... }
}
Pipes
{{ collectionOfBeers | orderBy:'note' | limitTo:5 }}
import {Pipe, PipeTransform} from 'angular2/core';
@Pipe({
name: 'uppercase'
})
export class UpperCasePipe implements PipeTransform {
transform(value: string, args: any[]) {
return value.toUpperCase();
}
}
import {Component} from 'angular2/core';
import {UpperCasePipe} from './UpperCasePipe'
@Component({
selector: 'widget1',
template: `<div>{{'Démo utilisant les pipes' | uppercase}}</div>`
})
export class Widget1{}
Démo
Questions ?