Angular 5 select 选项值强制为字符串
Angular 5 select option value forced to string
我定义了一个简单的 select
绑定到一个变量,如下所示:
<select id="client" name="client" [(ngModel)]="order.clientId">
<option *ngFor="let client of clients" [value]="client.id">
{{ client.name }}
</option>
</select>
并且 clients
是一个简单的 class,带有数字 id
值:
export class NameAndId {
id: number;
name: string;
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
}
所以我希望该值是一个数字,而不是一个字符串。并且 order.clientId
也被定义为一个数字。但是,当我像这样通过 HttpClient
post 调用传递 order
对象时,它将值编码为字符串:
return this.http.post<HttpResponse<any>>(this.baseUrl, order, {observe: 'response'});
为什么不显示为数值?
尝试使用 ngValue 代替 [value]
我定义了一个简单的 select
绑定到一个变量,如下所示:
<select id="client" name="client" [(ngModel)]="order.clientId">
<option *ngFor="let client of clients" [value]="client.id">
{{ client.name }}
</option>
</select>
并且 clients
是一个简单的 class,带有数字 id
值:
export class NameAndId {
id: number;
name: string;
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
}
所以我希望该值是一个数字,而不是一个字符串。并且 order.clientId
也被定义为一个数字。但是,当我像这样通过 HttpClient
post 调用传递 order
对象时,它将值编码为字符串:
return this.http.post<HttpResponse<any>>(this.baseUrl, order, {observe: 'response'});
为什么不显示为数值?
尝试使用 ngValue 代替 [value]