html - 无法在下拉框中设置默认选定选项
问题描述
如何在下拉框中将默认值设置为“我感兴趣”。这是代码。我也尝试删除 name 属性,但这不是一个好的解决方案。
<div class="form-group">
<select (blur) = "validateTopic(topic.value)" (change)="validateTopic(topic.value)" class="custom-select form-control" #topic="ngModel" [class.is-invalid]="topic.invalid && topic.touched" name="topic" [(ngModel)]="userModel.topic" required [value]="'I am interested in'">
<option value='default' selected>I am interested in </option>
<option *ngFor="let sub of topics">{{sub}}</option>
</select>
<small class="text-danger" [class.d-none]="!topicHasError && topic.touched">Please choose a topic</small>
</div>
默认情况下,下拉框中没有选择任何内容。
解决方案
<div class="form-group">
<select (blur) = "validateTopic(topic.value)" (change)="validateTopic(topic.value)" class="custom-select form-control" #topic="ngModel" [class.is-invalid]="topic.invalid && topic.touched" name="topic" [(ngModel)]="userModel.topic" required [value]="'default'">
<option value='default' selected>I am interested in </option>
<option *ngFor="let sub of topics" [value]="sub">{{sub}}</option>
</select>
<small class="text-danger" [class.d-none]="!topicHasError && topic.touched">Please choose a topic</small>
</div>
推荐阅读
- python - 根据内容查找 Python 文件
- vba - 由于 Private Declare 语句的位置导致编译错误
- powershell - 长时间运行任务后无法移动文件
- angular - 在 mat-table 中使用组件
- xml - 使用 powershell 将具有多个值的元素添加到现有的 xml 文件
- java - 我无法使用网格窗格正确地将图像放到窗口中
- .net-core - 在控制台应用程序中使用 EF Core 注入和迁移
- r - 使用差异随机化测试创建 R 函数
- python-3.x - TensorFlow 使用经过训练的模型进行检测
- javascript - 未处理的拒绝错误:ETELEGRAM:400 错误请求:消息文本为空