1 回答
TA贡献1829条经验 获得超7个赞
您需要将 html 更改为:
<form [formGroup]="cart_form" >
<select formControlName="category" (change)="onSelect($event.target.value)">
<option [ngValue]="null">--Select--</option>
<option *ngFor="let item of categories">{{item.name}}</option>
</select>
<div *ngIf="!cart_form.controls.category.valid" class="invalid-feedback">
<div *ngIf="cart_form.controls.category.errors.required">Title is required</div>
</div>
<button type="submit" [disabled]="cart_form.invalid">Submit</button>
</form>
你的组件
categories = [ {id: 1, name : 'test1'}, {id: 2, name : 'test12'}];
selectedCategory = null;
cart_form = new FormGroup({
category: new FormControl(null, Validators.required),
});
我为你构建了一个演示 https://stackblitz.com/edit/input-ngmodel-pnm31t?file=app/app.module.ts
- 1 回答
- 0 关注
- 78 浏览
添加回答
举报