繁体   English   中英

Angular 9 应用程序错误:在 Material Design 对话框中渲染表单字段失败

[英]Angular 9 app bug: rendering form fields in a Material Design Dialog fails

我正在开发 Angular 9 和 PHP 中的“任务”应用程序。 我在尝试使用数据预填充更新表单时遇到了Cannot find control with name: <controll name>错误。

表单模板:

<form [formGroup]="updateTask" name="edit_task_form">
  <mat-form-field appearance="standard">
    <mat-label>Title</mat-label>
    <input matInput placeholder="Title" formControlName="title" placeholder="Title">
  </mat-form-field>

  <mat-form-field appearance="standard">
    <mat-label>Short Description</mat-label>
    <input matInput placeholder="Short description" formControlName="short-description" placeholder="Short Description">
  </mat-form-field>

  <mat-form-field>
    <mat-label>Category</mat-label>
    <mat-select formControlName="tags">
      <mat-option *ngFor="let category of categories" [value]="category.id">{{category.name | titlecase}}</mat-option>
    </mat-select>
  </mat-form-field>

  <mat-form-field appearance="standard">
    <mat-label>Full Description</mat-label>
    <textarea matInput formControlName="full-description" placeholder="Full Description"></textarea>
  </mat-form-field>

  <div class="text-center">
    <button mat-flat-button type="submit" color="accent" [disabled]="updateTask.pristine || updateTask.invalid">Update</button>
  </div>
</form> 

在组件的 .ts 文件中:

task_hash: string;
currentTask: any = {};

constructor(private _apiService: ApiService, private _formBuilder: FormBuilder, private _sharedService: SharedService) {}

updateTask = this._formBuilder.group({});

ngOnInit(): void {

    this.task_hash = this._apiService.task_hash;

    this._apiService.getTaskInfo().subscribe(res => {
        this.currentTask = res;
        const formInfo = this.currentTask.info;

        formInfo.forEach(item => {
            if (item.key === 'title' || item.key === 'short-description' || item.key === 'description' || item.key === 'tags') {
                this.updateTask.addControl(item.key, this._formBuilder.control(item.data, item.key !== 'tags' ? Validators.required : null));
            };
        });
    });
}

在浏览器控制台中,我收到此错误(对于每个表单字段): Cannot find control with name: 'title'

此问题的原因似乎是,在另一个组件中,我在单击按钮触发的Angular 材质对话框中打开表单:

<button mat-button color="primary" (click)="openForm($event, task.task_hash)">Open</button>

In the trigger's .ts file

openEditForm(event, task_hash): void {

    event.stopPropagation();

    // Dialog Configuration
    const dialogConfig = new MatDialogConfig();
    dialogConfig.width = '60%';

    // Dialog Open
    this._matDialog.open(TaskFormComponent, dialogConfig);

    // Pass test_hash to API Service
    this.apiService.test_hash = test_hash;
}

为了使上面的对话框正常工作,我还在 TaskModule 中添加了这个

entryComponents: [TestFormComponent];

但似乎对话框在填充表单之前打开,这就是表单永远不会被填充的原因。

我该如何解决这个问题?

this._apiService.getTaskInfo()解决响应之前,我在模板中看不到任何阻止渲染<form [formGroup]="updateTask" name="edit_task_form">的条件,因此您收到错误 formControl not found。

getTaskInfo()订阅中添加一个标志

... this._apiService.getTaskInfo().subscribe(res => {... this.formLoaded = true; ... }...

并在模板中添加

<form *ngIf="formLoaded" [formGroup]="updateTask" name="edit_task_form">... ... </form>

这里找到stackblitz中的演示代码(请不要介意html,只是从问题中复制的)

在组件 class 上实现ngAfterViewInit接口:

@component
class AfterViewInitComponent implements AfterViewInit {

    ngAfterViewInit: void {
        this.task_hash = this._apiService.task_hash;

    this._apiService.getTaskInfo().subscribe(res => {
        this.currentTask = res;
        const formInfo = this.currentTask.info;

        formInfo.forEach(item => {
            if (item.key === 'title' || item.key === 'short-description' || item.key === 'description' || item.key === 'tags') {
                this.updateTask.addControl(item.key, this._formBuilder.control(item.data, item.key !== 'tags' ? Validators.required : null));
            };
        });
    });
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM