angular - 使用 Angular 6 选择下拉菜单的默认值
问题描述
我有一个选择输入:
<select id="duration" name="duration" class="form-control select-lg"
[(ngModel)]="championship.settings.fightDuration">
<option *ngFor="let duration of durations"
[selected]="championship.settings.fightDuration==duration">
{{ duration }} {{ championship.settings.fightDuration==duration }}
</option>
</select>
我有他的价值championship.settings.fightDuration
我尝试使用以下方法预先选择它:
[selected]="championship.settings.fightDuration==duration"
我在选择文本中,打印此条件的结果,并打印true
在与值匹配的元素上,因此它应该可以工作,但是选择没有选定的值...
我错过了什么?
解决方案
如果有人仍然在选择默认值时遇到问题,请尝试此操作。
假设您希望选择的选项具有 value duration
。我假设这个选项已经在items
数组中。
HTML
<select name="someName" [(ngModel)]="someModel">
<option *ngFor="let key of items" [value]="key.value">{{ key.label }}</option>
</select>
TS
class someComponent Implements OnInit {
someModel;
ngOnInit() {
this.someModel = 'duration';
this.items = [
{value: 'duration', label : 'Duration'},
{value: 'other', label : 'Other'},
{value: 'someOther', label : 'Some Other'}
];
}
提交时
在您的提交函数中,只需像这样调用即可获取选定的值。
this.someModel
希望这可以帮助某人。
这段视频对我有帮助 - https://www.youtube.com/watch?v=8ZlrORYOl_0
推荐阅读
- python - 如何强制跟踪将我的程序作为模块而不是脚本运行?
- wpf - 抛出的异常:mscorlib.dll 中的 System.IO.FileNotFoundException 并且在 IsolatedStorage 的路径中找不到文件
- php - 多文件请求验证不起作用
- python - 为什么训练 VGG-16 时准确率没有太大变化?
- javascript - React Native AsyncStorage.getItem 返回未定义
- node.js - 如何在 express.js 服务器中 JSON.parse 的位置 0 处修复 JSON 中的意外令牌 B?
- katalon-studio - Unable to open browser and navigate to URL on remote Web Server through SauceLabs in Katalon
- python - opencv:显示来自 sqlite3 的图像
- git - 如果其中一个拉取请求需要另一个拉取请求以通过 CI 检查,则将两个拉取请求合并到基础分支中
- javascript - 基于参数的搜索不适用于移动设备