Two-way binding in Angular 2 with NgModel and mutating bound property?

前端 未结 2 1914
深忆病人
深忆病人 2020-12-19 03:51

I am using Angular 2 beta 5.

Here\'s my view:




        
相关标签:
2条回答
  • 2020-12-19 04:13

    For binding textarea values you can use the change-function:

    Template

    <textarea id="some-value" (change)="doTextareaValueChange($event)">{{textareaValue}}</textarea>
    

    Component

    export class AppComponent implements OnInit {
      private textareaValue = '';
      doTextareaValueChange(ev) {
        try {
          this.textareaValue = ev.target.value;
        } catch(e) {
          console.info('could not set textarea-value');
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-19 04:15

    Update

    Add ngDefaultControl

    <paper-input ngDefaultControl [(ng-model)]="address">
    

    See also https://github.com/angular/angular/issues/5360

    **

    I guess this is a current limitation of the ngModel implementation. It binds to the value field of the element but for textarea it's the text AFAIR field it should bind to. It seems the textarea fires an event that ngModel listens to which makes it work in one direction.

    You can work around this by implementing a custom ValueAccessor

    See also
    - ngModel custom ValuesAccessor
    - Angular 2 custom form input
    - Bind angular 2 model to polymer dropdown

    0 讨论(0)
提交回复
热议问题