如何将所需的验证应用于角度2的下拉列表

amo*_*mol 6 angular

我正在使用select html标签,我是绑定列表.如果未选择下拉列表并在其标题上显示"选择",则需要进行必要的验证.

GsM*_*tra 4

我已经使用了*ngIf语法,但您可以对下拉菜单提供验证,例如:

<div class="form-group">
    <div>
        <label for="country">Country*:</label>
    </div>
    <div ng-class="{'valid':country.$valid}">
        <select class="form-control" name='country' [(ngModel)]='fd.country' required>
            <option *ngFor="let obj of country" [ngValue]="obj">{{obj}}</option>
        </select>
    </div>
    <small *ngIf="(myForm._submitted && !country.valid && !fd.country) || (!country.valid && country.dirty) ">Required (Please select country).</small>
</div>
Run Code Online (Sandbox Code Playgroud)