Reputation: 4596
I have this code in my template:
<select [ngModel]="selectedSubSectionId" (ngModelChange)="onSubSectionChange($event)">
<option *ngFor="let subSection of event.subSections" [ngValue]="subSection.id">{{ subSection.name }}</option>
</select>
In my component:
public selectedSubSectionId: any;
public onSubSectionChange(subSectionId: any) {
// some code I execute after ngModel changes.
}
This works ok, but at the beginning I have an empty box. I want to show a placeholder message there. How can I do this using ngModel?
Upvotes: 22
Views: 44914
Reputation: 592
<option
[selected]="true" value="">Please select</option>
option
tag ` with Please select as the content.[selected]="true"
attribute.value = ""
as an attribute.Without value=""
the content is not shown until you click on the select field.
You can change [selected]="true"
to selected
.
<option
selected value="">Please select</option>
Upvotes: 0
Reputation: 590
I've the same question, and i found an example in this great website: Angular Quick Tip
also, i put the example below:
// template
<form #f="ngForm">
<select name="state" [ngModel]="state">
<option [ngValue]="null">Choose a state</option>
<option *ngFor="let state of states" [ngValue]="state">
{{ state.name }}
</option>
</select>
</form>
//component
state = null;
states = [
{name: 'Arizona', code: 'AZ'},
{name: 'California', code: 'CA'},
{name: 'Colorado', code: 'CO'}
];
Also works with Reactive Forms, that's what i'm using:
// template
<div class="form-group">
<select formControlName="category">
<option [ngValue]="null">Select Category</option>
<option *ngFor="let option of options"
[ngValue]="option">{{option.label}}</option>
</select>
</div>
// component
options = [{ id: 1, label: 'Category One' }, { id: 2, label: 'Category Two' }];
form = new FormGroup({
category: new FormControl(null, Validators.required)
});
Thanks to Netanel Basal to provide the answer
Upvotes: 14
Reputation: 1360
Add empty option and set to 'undefined' also can be add for null value too
<select [(ngModel)]="barcode">
<option value="undefined" disabled selected hidden>Select</option>
<option value="null" disabled selected hidden>Select</option>
<option *ngFor="let city of turkiye" [ngValue]="city.id">{{city.name}}</option>
</select>
Upvotes: 9
Reputation: 354
My solution:
In the component typescript file I add a property selectUndefinedOptionValue that I don't initialize and in the html I add the undefinedSelectOptionValue as value of the placeholder option. This solution works for both number and string model properties.
@Component({
selector: 'some-component-selector',
templateUrl:'url to template',
})
export class SomeComponent implements OnInit {
private selectUndefinedOptionValue:any;
private someObject:SomeObject;
ngOnInit() {
someObject = new SomeObject();
}
}
<select [(ngModel)]="someObject.somePropertyId">
<option disabled hidden [value]="selectUndefinedOptionValue">-- select --</option>
<option *ngFor="let option of options" [value]="option.id">option.text</option>
</select>
Upvotes: 33
Reputation: 241
try this code:
<select [ngModel]="selectedSubSectionId? selectedSubSectionId : ''" (ngModelChange)="onSubSectionChange($event)">
<option value="" disabled selected hidden>Placeholder</option>
<option *ngFor="let subSection of event.subSections" [value]="subSection.id">{{ subSection.name }}</option>
</select>
Upvotes: 6