angular - 给占位符在Angular 6中选择
问题描述
我有一个选择选项,我想在其中给出“选择一个类别”的占位符
<form role="form" class="form form-horizontal" (ngSubmit)="onSubmit()" #form="ngForm" ngNativeValidate>
<div class="form-group row">
<div class="col-xl-4 col-lg-6 col-md-12">
<fieldset class="form-group">
<label for="customSelect">Categories:</label>
<select class="custom-select d-block w-100" id="Category" [(ngModel)]="Category" name="Category" required>
<option value=" ">Select one category </option>
<option *ngFor="let item of myBusinessList" [value]="item.id">{{item.name}}</option>
</select>
</fieldset>
</div>
</div>
<button type="submit" class="btn btn-raised btn-danger">Save</button>
</form>
如果我删除 [ngModel] 那么它可以工作。如果我写
<option value="undefined" selected>Select one category </option>
然后它认为是价值之一。我必须确保有位置,并且需要选择其中一个值
解决方案
如果您希望在Category
is still时选择第一个值undefined
,您可以将值分配undefined
给第一个选项ngValue
:
<option [ngValue]="undefined" hidden>Select one category</option>
推荐阅读
- c# - 在 C# 中使用 Parallel.Foreach 时,我们可以控制 CPU 使用率吗?
- list - 使用列表理解计算列表中的元素
- python - Pandas:将语言环境设置为来自 CSV 文件的本地时间不明确的数据框
- javascript - 如何在保留一个特定值的同时将一组值减少一半?
- java - ZonedDateTime.parse 无法在索引 2 处解析
- html - 无法将图像作为css背景
- java - 如何选择颜色
- jenkins - Jenkins 不适用于 Amazon EC2 实例
- java - ResponseAdvice 没有被触发
- matlab - 如何使用切换按钮打开/关闭散点图中的标签