我正在从 Angular 5 迁移到 Angular 7,我发现在 Angular6 中不推荐使用 ngmodel 和 formcontrolName 在同一个元素中并在 7 中删除。现在我无法将验证器设置为来自 angular 材料的 mat-chip 输入
html:
<div class="form-group col-md-6">
<label class="required"> User Names
</label>
<mat-form-field >
<mat-chip-list class="form-control form-control-sm"
[ngClass]="{'is-invalid':form.controls.names.invalid && (form.controls.names.touched || form.controls.names.dirty) }"
#chipList3>
<mat-chip *ngFor="let local of form.get('names').value" [removable]="removable"
(remove)="remove_names(local)">
{{local}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input [matChipInputFor]="chipList3"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes" [matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add_names($event)" />
</mat-chip-list>
</mat-form-field>
</div>
在迁移到 Angular 7 之前,我会像这样在输入标签中使用 formControlName
<div class="form-group col-md-6">
<label class="required"> User Names
</label>
<mat-form-field >
<mat-chip-list class="form-control form-control-sm"
[ngClass]="{'is-invalid':form.controls.names.invalid && (form.controls.names.touched || form.controls.names.dirty) }"
#chipList3>
<mat-chip *ngFor="let local of user.names" [removable]="removable"
(remove)="remove_names(local)">
{{local}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input [matChipInputFor]="chipList3"
formControlName="names"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes" [matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add_names($event)" />
</mat-chip-list>
</mat-form-field>
</div>
当用户将名称推送到列表中时,我会执行所有自定义验证,但我想检查它是否存在,因为我使用 Validators.required 但现在因为我使用 formcontrol 值本身来显示列表,所以我不能给出任何引用模板中的formControlName
TS:
user :FormGroup =this.fb.group({
names:[[], [Validators.required]],
id:0
});
现在,即使 formControl 中有值,它也不满足 Validators.required
在花时间研究之后,我发现添加这个
this.form.controls['names'].updateValueAndValidity()
满意 Validators.required 但现在我收到此错误
Error: No value accessor for form control with name: 'names'