在Angular 8中,可以通过使用[(ngModel)]
指令来设置select标签的默认选中值。以下是一个示例:
[(ngModel)]
指令绑定select标签的选中值到组件中的一个变量。例如,假设我们有一个名为selectedValue
的变量来保存选中的值:
ngOnInit
生命周期钩子中设置selectedValue
的初始值。例如:selectedValue: string;
constructor() {
this.selectedValue = "option2"; // 设置默认选中值为"option2"
}
// 或者使用 ngOnInit 钩子
ngOnInit() {
this.selectedValue = "option2"; // 设置默认选中值为"option2"
}
以上代码将会在页面加载时将"Option 2"设置为select标签的默认选中值。根据需要,你可以将selectedValue
设置为任何选项的值来设置其他选项为默认选中值。