angular @Input与@Output 双向绑定

发布于 2021-03-31  605 次阅读


angular @Input@Output 双向绑定

@Input允许父组件更新子组件中的数据;
@Output允许子组件向父组件发送数据;

通过将同一个属性使用@Input@Output一起绑定,使得数据在父组件和子组件之间同步更新。

angular的双向绑定遵循如下格式:

@Component({
    selector:'app-test-component',
    templateUrl:'./test.html',
    styleUrls:['./test.css']
})
export class TestComponent{
    @Input() attri:type;
    @Output() attriChange = new EventEmitter<type>();

    notifyAttri(){
        this.attriChange.emit(this.attri);
    }
}
<app-test-component [(attri)]="attri"></app-test-component>

朝闻道,夕死可矣