two way binding with safe navigation operator

和自甴很熟 提交于 2019-12-27 12:23:17

问题


What is the best way to use two-way-binding (syntax-sugar) in Angular 2 with the safe navigation operator. I've tried the following.

<input [(ngModel)]="x?.y?.z"> 

But this is not supported.

Is there a way to use sth. like this?


回答1:


You can split up- and downwards-binding like

<input [ngModel]="x?.y?.z" (ngModelChange)="x?.y?.z ? x.y.z = $event : null"> 



回答2:


<input [ngModel]="x?.y?.z" (keyup)="changeMe($event.target.value)"> {{x?.y?.z}}

export class ParentCmp {
  x={y:{z:"a"}}
   changeMe(val)
    {
      console.log(val);
      this.x.y.z=val;
    }
}

http://plnkr.co/edit/ZBeSPqf4HUwLOeWSNfZJ?p=preview



来源:https://stackoverflow.com/questions/36016407/two-way-binding-with-safe-navigation-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!