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

时间:2018-11-18 00:59:22

标签: angular typescript angular-httpclient

我已经定义了一个简单的select绑定到这样的变量:

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

clients是一个简单的类,具有数字id值:

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

    constructor(id: number, name: string) {
        this.id = id;
        this.name = name;
    }
}

因此,我希望该值是数字,而不是字符串。 order.clientId也定义为数字。但是,当我像这样通过order发布调用传递HttpClient对象时,它会将值编码为字符串:

return this.http.post<HttpResponse<any>>(this.baseUrl, order, {observe: 'response'});

为什么它不显示为数字值?

1 个答案:

答案 0 :(得分:0)

尝试使用ngValue代替[value]

Stackblitz Example