Angular 7 中可能为空的输入字段的设置值
Setting values of Input fields with possible null in Angular 7
我正在使用以下方法在输入字段中放置值。
<input matInput placeholder = "ProductName" [(ngModel)]="orderLine.productname">
但是,有时该值可能为空。什么时候打问号?下面,我们收到以下错误,有人如何在输入中放置可能的空白 null 值,或者不可能?
<input matInput placeholder = "ProductName" [(ngModel)]="orderLine?.productname">
The '?.' operator cannot be used in the assignment at column in [[orderLine?.productname=$event]
您需要将双向绑定拆分为一个数据和一个事件绑定:-
[ngModel] = "orderLine?.price"
(ngModelChange) = "orderLine.price = $event"
你不能在 ngModel 双向绑定中使用 ?.
,也许你可以使用三元运算符:
<input matInput placeholder = "Price" [(ngModel)]="orderLine? orderLine.price: null">
我们不能在双向绑定中使用三元运算符?
<input matInput placeholder = "Price" [(ngModel)]="orderLine?.price">
如果您的 orderLine
对象如下所示,那么 您不需要使用 这个运算符...
orderLine= new OrderLine();
class OrderLine {
...
price: number;
...
}
请将其分成两部分,因为这两部分是 ngModel::
的一部分
[ngModel]="orderLine?.price" (ngModelChange)="orderLine.price = $event"
您需要拆分双向绑定。
[ngModel]="orderLine?.price" (ngModelChange)="orderLine.price = $event"
我正在使用以下方法在输入字段中放置值。
<input matInput placeholder = "ProductName" [(ngModel)]="orderLine.productname">
但是,有时该值可能为空。什么时候打问号?下面,我们收到以下错误,有人如何在输入中放置可能的空白 null 值,或者不可能?
<input matInput placeholder = "ProductName" [(ngModel)]="orderLine?.productname">
The '?.' operator cannot be used in the assignment at column in [[orderLine?.productname=$event]
您需要将双向绑定拆分为一个数据和一个事件绑定:-
[ngModel] = "orderLine?.price"
(ngModelChange) = "orderLine.price = $event"
你不能在 ngModel 双向绑定中使用 ?.
,也许你可以使用三元运算符:
<input matInput placeholder = "Price" [(ngModel)]="orderLine? orderLine.price: null">
我们不能在双向绑定中使用三元运算符?
<input matInput placeholder = "Price" [(ngModel)]="orderLine?.price">
如果您的 orderLine
对象如下所示,那么 您不需要使用 这个运算符...
orderLine= new OrderLine();
class OrderLine {
...
price: number;
...
}
请将其分成两部分,因为这两部分是 ngModel::
的一部分[ngModel]="orderLine?.price" (ngModelChange)="orderLine.price = $event"
您需要拆分双向绑定。
[ngModel]="orderLine?.price" (ngModelChange)="orderLine.price = $event"