1

I am trying to add a select that shows a placeholder by default like this:

<select id="type" formControlName="type">
  <option hidden value="" [disabled]="true" [selected]="true">Placeholder</option>
  <option *ngFor="let item of ['test 1', 'test 2']" [value]="item">{{ item }}</option>
</select>

I don't see Placeholder in the select when I load the page. But if I do:

<select id="type" formControlName="type">
  <option hidden value="" [disabled]="true" [selected]="true">Placeholder</option>
  <option>test 1</option>
  <option>test 2</option>
</select>

I see it. What is wrong with *ngFor that it does not show the placeholder?

2
  • I don't see any issue with your code, I can see a placeholder both ways. See here, I have used your code as it is - stackblitz.com/edit/… Kindly create a working example reproducing your issue. Commented Dec 2, 2021 at 17:59
  • Does this answer your question ? stackoverflow.com/a/68849750/6444705 Commented Dec 3, 2021 at 8:35

2 Answers 2

0

I faced similar problems with the select placeholder and each time had different solutions working. Try just leaving the disabled and selected (remove the hidden) or maybe assign to your FormControlName an initial dummy value like -1 and then set the placeholder option value to -1.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm using A14 and NonNullableFormBuilder. There I set the default value for the formControlName to -1 when initializing the control on the FormBuilder and then add -1 to the value attribute of the default option to show. Then you can set a validator value for checking if the value is higher than -1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.