Give placeholder to select in Angular 6
simply use [value]="undefined"
<select class="form-control" [(ngModel)]="selectedCategory">
<option [value]="undefined" disabled hidden>Select...</option>
<option *ngFor="let c of categories">{{c}}</option>
</select>
for a template driven angular form you may want to consider this demo. you can find the code here
for a simple html form here is a snippet.
<form>
<select required>
<option value="" disabled selected hidden>Select a value</option>
<option value="0">option 1</option>
<option value="1">option 2</option>
</select>
</form>
<option value="" disabled selected hidden>Select your option</option>
You can use [value]="" selected hidden
I have create a demo on Stackblitz
<form role="form" class="form form-horizontal" (ngSubmit)="onSubmit()" #form="ngForm" ngNativeValidate>
<div class="form-group row">
<div class="col-xl-4 col-lg-6 col-md-12">
<fieldset class="form-group">
<label for="customSelect">Categories:</label>
<select class="custom-select d-block w-100" id="Category" [(ngModel)]="Category" name="Category" required placeholder="d.ff">
<option hidden [value]="" selected>Select one category </option>
<option *ngFor="let item of myBusinessList" [value]="item.id">{{item.name}}</option>
</select>
</fieldset>
</div>
</div>
<button type="submit" class="btn btn-raised btn-danger">Save</button>
</form>
If you want the first value to be selected when Category
is still undefined
, you can assign the value undefined
to the first option with ngValue
:
<option [ngValue]="undefined" hidden>Select one category</option>