Search code examples
angulartypescriptangular-materialangular7

Setting values of Input fields with possible null in Angular 7


I am using following to place values in an Input field. Setting values of input fields with Angular 6

<input matInput placeholder = "ProductName" [(ngModel)]="orderLine.productname">

However, sometimes the value maybe null. When placing a question mark ? below, we receive the following error, how does someone place possible blank null value in input, or is not possible?

<input matInput placeholder = "ProductName" [(ngModel)]="orderLine?.productname">

The '?.' operator cannot be used in the assignment at column in [[orderLine?.productname=$event]


Solution

  • Please split it into two parts as these two are part of ngModel::

    [ngModel]="orderLine?.price" (ngModelChange)="orderLine.price = $event"