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?
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>
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
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>
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>
Have you tried putting placeholder="Your placeholder string"
inside the select or option tag?
I assume the string can be replaced with a variable if you don't want to hard code the string in your view.
来源:https://stackoverflow.com/questions/38725365/how-to-show-placeholder-empty-option-in-select-control-in-angular-2