Implement two-way data binding for custom property

后端 未结 4 436
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-12 13:29

I see in primeng components the use something like ngModel (two-way data binding) style for some property like this

[(selection)]=\"selectedItem\";
<         


        
4条回答
  •  时光说笑
    2021-01-12 14:15

    In your child component you have to implement two-way binding interface like this:

    private _selection: any;
    get selection(): any {
        return this._selection;
    }
    @Input()
    set selection(value: any) {
        if(this._selection === value) {
            return;
        }
        this._selection = value;
        this.selectionChange.emit(this._selection);
    }
    @Output()
    selectionChange = new EventEmitter();
    

    It's mandatory to name @Output filed by adding propertyNameChange to @Input name. So you can use it in your parent component temlate like this:

    
    

提交回复
热议问题