Gargoyle
Gargoyle

Reputation: 10345

Angular 5 select option value forced to string

I've defined a simple select bound to a variable like so:

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

and the clients is a simple class with a numeric id value:

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

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

So I would expect that the value is a number, not a string. And the order.clientId is also defined as a number. However, when I pass the order object through an HttpClient post call like so, it's encoding the value as a string:

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

Why isn't it appearing as a numeric value?

Upvotes: 2

Views: 2203

Answers (1)

Gabriel Lopez
Gabriel Lopez

Reputation: 452

Try ngValue instead of [value]

Stackblitz Example

Upvotes: 8

Related Questions