我认为"value"在<select>下面的元素上添加属性集会导致默认选择<option>包含my provided "value":
<select name="hall" id="hall" value="3">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>Run Code Online (Sandbox Code Playgroud)
但是,这并没有像我预期的那样奏效.如何设置<option>默认选择的元素?
我在我的模板中有这个代码:
<select [ngModel]="selectedSubSectionId" (ngModelChange)="onSubSectionChange($event)">
<option *ngFor="let subSection of event.subSections" [ngValue]="subSection.id">{{ subSection.name }}</option>
</select>
Run Code Online (Sandbox Code Playgroud)
在我的组件中:
public selectedSubSectionId: any;
public onSubSectionChange(subSectionId: any) {
// some code I execute after ngModel changes.
}
Run Code Online (Sandbox Code Playgroud)
这样可以,但最初我有一个空盒子.我想在那里显示占位符消息.如何使用ngModel执行此操作?