1. input properties, typically adorned with @Input decorations.
-
import{ Component,Input} from'@angular/core';
-
-
import{ Hero} from'./hero';
-
-
@Component({
-
selector:'hero-child',
-
template:`
-
<h3>{{hero.name}} says:</h3>
-
<p>I, {{hero.name}}, am at your service, {{masterName}}.</p>
-
`
-
})
-
exportclassHeroChildComponent{
-
@Input() hero: Hero;
-
@Input('master') masterName: string;
-
//The second
@Input
aliases the child component property namemasterName
as'master'
. -
}
-
2.在父组件就可以这样使用了
-
-
import{ Component} from'@angular/core';
-
-
import{ HEROES }from'./hero';
-
-
@Component({
-
selector:'hero-parent',
-
template:`
-
<h2>{{master}} controls {{heroes.length}} heroes</h2>
-
<hero-child *ngFor="let hero of heroes"
-
[hero]="hero"
-
[master]="master">
-
</hero-child>
-
`
-
})
-
exportclassHeroParentComponent{
-
heroes= HEROES;
-
master:string= 'Master';
-
}
-
- 1.
-
-
import { Component, Input } from '@angular/core';
-
-
@Component({
-
selector: 'name-child',
-
template: '<h3>"{{name}}"</h3>'
-
})
-
export class NameChildComponent {
-
private _name = '';
-
-
@Input()
-
set name(name: string) {
-
this._name = (name && name.trim()) || '<no name set>';
-
}
-
-
get name(): string { return this._name; }
-
}
-
-
-
import { Component } from '@angular/core';
-
-
@Component({
-
selector: 'name-parent',
-
template: `
-
<h2>Master controls {{names.length}} names</h2>
-
<name-child *ngFor="let name of names" [name]="name"></name-child>
-
`
-
})
-
export class NameParentComponent {
-
// Displays 'Mr. IQ', '<no name set>', 'Bombasto'
-
names = ['Mr. IQ', ' ', ' Bombasto '];
-
}
-