Ric*_*ino 7 angular-material angular angular7
我正在使用反应式表单,我似乎遇到了看似随机的表单字段的问题。关于为什么会发生这种情况的任何想法都是合理的。
如果有帮助,我刚刚开始使用 angular 和 material 7
有趣的是,在表单中添加和删除元素会导致其他元素出现问题。
ERROR 错误:没有未指定名称属性的表单控件的值访问器
TS
export class VolunteerApplicationPersonalStepComponent implements OnInit
{
public readonly form: FormGroup;
constructor(private fb: FormBuilder) {
this.form = this.fb.group({
businessTelephoneExt: [''],
otherTelephone: [''],
otherTelephoneExt: [''],
});
}
}
Run Code Online (Sandbox Code Playgroud)
HTML
<form [formGroup]="form">
<mat-form-field>
<input matInput i18n-placeholder placeholder="Business Extension"
[formControl]="form.get('businessTelephoneExt')">
</mat-form-field>
<app-telephone-input i18n-placeholder placeholder="Other Telephone"
[formControl]="form.get('otherTelephone')">
</app-telephone-input>
<mat-form-field>
<input matInput i18n-placeholder placeholder="Other Extension"
[formControl]="form.get('otherTelephoneExt')">
</mat-form-field>
<br>
<div class="group-margin group-min-width">
<button mat-stroked-button color="primary" matStepperPrevious i18n>Previous</button>
<button mat-raised-button color="primary" matStepperNext (click)="next()" i18n>Next</button>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
正如有人建议的 .. formControlName="businessTelephoneExt"
应用程序电话代码(注意它曾经有 formControl 而不是 appFormControl)
export class TelephoneInputComponent implements OnInit {
@Input() public required = false;
@Input() public placeholder = '';
@Input() public appFormControl: NgControl;
constructor() {
}
public ngOnInit() {
}
}
<mat-form-field>
<input
matInput
type="tel"
[required]="required"
[placeholder]="placeholder"
[formControl]="appFormControl">
<mat-hint i18n>Please enter digits only</mat-hint>
<mat-error
*ngIf="appFormControl.hasError('phone')"
i18n>Invalid phone (requires 10 digits)
</mat-error>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20811 次 |
| 最近记录: |