Angular 5选择选项值强制字符串

Gar*_*yle 1 typescript angular angular-httpclient

我定义了一个简单的select变量绑定,如下所示:

<select id="client" name="client" [(ngModel)]="order.clientId">
    <option *ngFor="let client of clients" [value]="client.id">
        {{ client.name }}
    </option>
</select>
Run Code Online (Sandbox Code Playgroud)

clients是一个带有数值的简单类id

<select id="client" name="client" [(ngModel)]="order.clientId">
    <option *ngFor="let client of clients" [value]="client.id">
        {{ client.name }}
    </option>
</select>
Run Code Online (Sandbox Code Playgroud)

所以我希望该值是一个数字,而不是一个字符串。并且order.clientId也被定义为一个数字。但是,当我像这样order通过HttpClientpost 调用传递对象时,它将值编码为字符串:

export class NameAndId {
    id: number;
    name: string;

    constructor(id: number, name: string) {
        this.id = id;
        this.name = name;
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么它不显示为数值?

小智 8

尝试使用 ngValue 而不是 [value]

堆栈闪电战示例