我猜双向绑定应该可以工作Plunkr:
指示
@Directive({
selector: '[mousePosCollector]'
})
export class MousePosCollectorDirective {
@Input() x;
@Output() xChange = new EventEmitter();
ngOnInit() {
setTimeout(() => {
this.x = ++this.x;
this.xChange.emit(this.x);
}, 1000)
}
ngOnChanges() {
console.info(`Changes from MousePosCollectorDirective: ${this.x}`);
}
}
零件
@Component({
selector: 'tab',
template: `<h3>tab {{x}}</h3>`
})
export class TabComponent {
@Input() x;
@Output() xChange = new EventEmitter();
ngOnInit() {
setTimeout(() => {
this.x = ++this.x;
this.xChange.emit(this.x);
}, 2000)
}
ngOnChanges() {
console.info(`Changes from TabComponent: ${this.x}`);
}
}
父组件
@Component({
selector: 'my-app',
template: `
<tab mousePosCollector [(x)]="x"></tab>
{{x}}`
})
export class AppComponent {
x = 1;
ngOnInit() {
setTimeout(() => {
this.x = ++this.x;
}, 3000)
}
}