在这里你必须设置@Output并更改组件
export class CounterComponent {
@Output('initoChange') emitter: EventEmitter<string> = new EventEmitter<string>();
@Input('inito') set setInitoValue(value) {
this.count = value;
}
count: number = 0;
increment() {
this.count++;
this.emitter.emit(this.count);
}
decrement() {
this.count--;
this.emitter.emit(this.count);
}
}
Run Code Online (Sandbox Code Playgroud)
这是与plunker的链接,请看看.
您可以进行双向数据绑定,如下所示:
@Component({
selector: 'app-sizer',
template: `
<div>
<button (click)="dec()" title="smaller">-</button>
<button (click)="inc()" title="bigger">+</button>
<label [style.font-size.px]="size">FontSize: {{size}}px</label>
</div>`
})
export class SizerComponent {
@Input() size: number | string;
@Output() sizeChange = new EventEmitter<number>();
dec() { this.resize(-1); }
inc() { this.resize(+1); }
resize(delta: number) {
this.size = Math.min(40, Math.max(8, +this.size + delta));
this.sizeChange.emit(this.size);
}
}
Run Code Online (Sandbox Code Playgroud)
并在父组件的模板中进行双向绑定,size如下所示:
<app-sizer [(size)]="fontSizePx"></app-sizer>
<div [style.font-size.px]="fontSizePx">Resizable Text</div>
Run Code Online (Sandbox Code Playgroud)
它(双向绑定)只是属性绑定的语法糖,所以它等价于:
<app-sizer [size]="fontSizePx" (sizeChange)="fontSizePx=$event"></app-sizer>
Run Code Online (Sandbox Code Playgroud)
[(prop)]当调用prophasInput属性prop并且Output具有 name 的事件(属性)时,语法 using 是可能的propChange。
代码来自 angular docs 有关更多信息,请导航到此地址:https : //angular.io/guide/template-syntax#two-way-binding---
| 归档时间: |
|
| 查看次数: |
8841 次 |
| 最近记录: |