Implement two-way data binding for custom property

后端 未结 4 441
爱一瞬间的悲伤
爱一瞬间的悲伤 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:14

    Angular docs

    
    
    

    The two-way binding syntax is really just syntactic sugar for a property binding and an event binding. Angular desugars the binding into this:

    
    
    

    To create two-way binding for property selection use:

    @Input() selection;
    
    // You have to follow this convention (For Output property)
    // i.e.  like selectionChange
    
    @Output() selectionChange:EventEmitter = new EventEmitter();
    

    And to change selection in component as shown below:

    changeSelection(newSelection)
    {
        this.selection = newSelection;
    
        // emit change event to parent component
        this.selectionChange.emit(this.selection);  
    }
    

提交回复
热议问题