首页 > 解决方案 > 给占位符在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>    

然后它认为是价值之一。我必须确保有位置,并且需要选择其中一个值

标签: angularselectdropdown

解决方案


如果您希望在Categoryis still时选择第一个值undefined,您可以将值分配undefined给第一个选项ngValue

<option [ngValue]="undefined" hidden>Select one category</option>

推荐阅读