Architecture composant
Petite app = petite vue + petit controller
Grosse vue
Gros controlleur
Grosse application
Enorme vue
Enorme controlleur
Enorme application
Large app 1.x
Routes
nommées
Routes
nommées
Routes
nommées
Large app 1.x
Directive
Directive
Directive
Toutes les app ng2
Component
Component
Component
<div>
<item-detail
(saved)="saveItem($event)"
(cancelled)="resetItem($event)"
[item]="selectedItem">Select an Item</item-detail>
</div>
import { Component, Input } from 'angular2/core';
@Component({
selector: 'my-component',
template: `
<div>Greeting from parent:</div>
<div>{{greeting}}</div>
`
})
export class MyComponent {
@Input() greeting: String = 'Default Greeting';
}
import { Component } from 'angular2/core'
import { MyComponent } from './components/my.component'
@Component({
selector: 'app',
template: `
<my-component [greeting]="greeting"></my-component>
<my-component></my-component>
`,
directives: [ MyComponent ]
})
export class App {
greeting: String = 'Hello child!'
}
import { Component, Output, EventEmitter } from 'angular2/core'
@Component({
selector: 'my-component',
template: `<button (click)="greet()">Greet Me</button>`
})
export class MyComponent {
@Output() greeter: EventEmitter = new EventEmitter()
greet() {
this.greeter.emit('Child greeting emitted!')
}
}
import {MyComponent} from './my-component'
@Component({
selector: 'app',
template: `
<div>
<h1>{{greeting}}</h1>
<my-component (greeter)="greet($event)"></my-component>
</div>
`,
directives: [MyComponent]
})
export class App {
greet(event) {
this.greeting = event;
}
}
export class ItemsList {
@Input() items: Item[];
@Output() selected = new EventEmitter();
@Output() deleted = new EventEmitter();
}
export class App implements OnInit {
items: Array<Item>;
selectedItem: Item;
constructor(private itemsService: ItemsService) {}
ngOnInit() { }
resetItem() { }
selectItem(item: Item) { }
saveItem(item: Item) { }
replaceItem(item: Item) { }
pushItem(item: Item) { }
deleteItem(item: Item) { }
}
import { Component, Output, EventEmitter } from 'angular2/core'
@Component({
selector: 'my-component',
template: `<button (click)="greet()">Greet Me</button>`,
encapsulation: ViewEncapsulation.None // or ViewEncapsulation.Emulated or ViewEncapsulation.Native
})
export class MyComponent {
@Output() greeter: EventEmitter = new EventEmitter()
greet() {
this.greeter.emit('Child greeting emitted!')
}
}