nunoarruda
nunoarruda

Reputation: 2904

Why ngModel adds a blank/empty option to a select?

I use the name attribute and the ngModel directive as an easy way to get form values in an Angular 2 app:

<input type="text" name="firstName" ngModel>

But I'm having a problem when using this approach in a select:

<select name="gender" ngModel>

The select with a ngModel renders with a blank/empty option.

Without ngModel:

enter image description here

With ngModel:

enter image description here

Here's a plunker: http://plnkr.co/edit/hwF2U7WHp1U8IQnVGEXv?p=preview

Am I missing something or is this a bug?

Upvotes: 3

Views: 1261

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657008

If you use

<select name="gender" [(ngModel)]="gender">

with

gender: string = 'male';

You get the desired result. If you use ngModel this way, you also shouldn't need the getFormData method.

Plunker example

Upvotes: 1

Related Questions