<form [formGroup]="addFeedForm" (ngSubmit)="addFeed(addFeedForm.value)" class="col s12">
<div class="row">
<div class="input-field col m6 s12">
<select formControlName="departmentId" #feedDepartment>
<option value=0>General</option>
<option [value]="department.id" *ngFor="let department of departmentList">{{department.name}}</option>
</select>
<label>Department</label>
</div>
<div>
<button type="button" class="btn waves-effect waves-light col s3 offset-s5" (click)="resetAddFeedForm()">Reset</button>
<button type="submit" [disabled]="!addFeedForm.valid" class="btn waves-effect waves-light col s3 offset-s1">Add</button>
</div>
</div>
</form>
这就是我创建 Angular 表单的方式
this.addFeedForm = new FormGroup({
departmentId: new FormControl(0,Validators.compose([Validators.required])),
});
当我提交时,我会这样做
addFeed(data: any): void {
console.log(data);
this.addFeedForm.reset({ departmentId: 0 });
}
如果我更改了部门选择选项并提交,然后通过将departmentId
设置为0来重置表单,则应在选择选项中选择“General”。
它在控制台上设置departmentId
为0,但UI上没有更新。
在UI上选择的选项不同,但在控制台上departmentId
为0。
更新:这是stackblitz链接
materialize-css
的工作方式有关。例如,请查看https://dev59.com/510a5IYBdhLWcg3wboIv。双向数据绑定似乎不起作用,你需要手动完成很多工作。如果您的整个UI都是使用Materialize构建的,则使用Angular Materialize包装器[ngx-materialize]可能更容易。 - frido