angular - @ViewChild 以子组件形式返回 undefined reference

标签 angular angular-forms

<分区>

我在父组件中有一个表单将数据作为输入属性传输到子组件,而子组件有一个表单读取此数据并使用此数据预填充其表单作为编辑用户配置文件的一种方式。在我尝试使用传输的数据设置输入字段的行中,chrome 返回一个控制台错误,指出它无法读取未定义的 setValue 属性。代码可能有什么问题?

这是代码:

this.eForm.setValue({
        firstname: this.employee.user.firstname,
        lastname: this.employee.user.lastname,
        email: this.employee.user.email
      });

子组件:

import { Component, OnInit, ViewChild, Input } from '@angular/core';
import { DataService } from 'src/app/services/data.service';
import { NgForm, ControlContainer } from '@angular/forms';

@Component({
  selector: 'app-manager-view-employee-profile',
  templateUrl: './manager-view-employee-profile.component.html',
  styleUrls: ['./manager-view-employee-profile.component.css'],
  viewProviders: [ { provide: ControlContainer, useExisting: NgForm } ]
})
export class ManagerViewEmployeeProfileComponent implements OnInit {
  @ViewChild('f', {static: false}) eForm: NgForm;

  @Input() employee: any;

  constructor(private dataService: DataService) { }

  ngOnInit() {

    console.log(this.eForm);
    console.log(this.employee.user.firstname);
this.eForm.setValue({
    firstname: this.employee.user.firstname,
    lastname: this.employee.user.lastname,
    email: this.employee.user.email
  });
  }

  onSubmit(f: NgForm){



    }
}

子组件模板:

<form class="col-md-6" (ngSubmit)="onSubmit(f)" #f="ngForm">
        <div class="form-group">
          <div class="input-group mb-2">
            <div class="input-group-prepend">
              <div class="input-group-text">First Name</div>
            </div>
            <input type="text" class="form-control" id="inlineFormInputGroup" name="firstname" ngModel required>
            <span *ngIf="f.form.controls.firstname?.touched && !f.form.controls.firstname?.valid" style="color: red;">A is required.</span>
          </div>

        </div>

        <div class="form-group">
            <div class="input-group mb-2">
              <div class="input-group-prepend">
                <div class="input-group-text">Last Name</div>
              </div>
              <input type="text" class="form-control" id="inlineFormInputGroup" name="lastname" ngModel required>
              <span *ngIf="f.form.controls.lastname?.touched && !f.form.controls.lastname?.valid" style="color: red;">A is required.</span>
            </div>

          </div>


        <div class="form-group">
            <div class="input-group mb-2">
              <div class="input-group-prepend">
                <div class="input-group-text">Email</div>
              </div>
              <input type="text" class="form-control" id="inlineFormInputGroup" name="email" ngModel required>
              <span *ngIf="f.form.controls.email?.touched && !f.form.controls.email?.valid" style="color: red;">An email is required.</span>
            </div>

          </div>

        <button type="submit" class="btn btn-primary" [disabled]="!f.form.valid">Search</button>
      </form>

最佳答案

// query results available in ngOnInit
@ViewChild('f', {static: true}) eForm: NgForm;

或者


ngAfterViewInit() {

this.eForm.setValue({
    firstname: this.employee.user.firstname,
    lastname: this.employee.user.lastname,
    email: this.employee.user.email
  });

}

https://angular.io/guide/static-query-migration

关于angular - @ViewChild 以子组件形式返回 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58033379/

相关文章:

angular - 在特征内选择数据的 ngrx/entity 问题

javascript - 如何使用 angular2-moment 显示 1 分钟前的时间

unit-testing - 模拟服务时 Angular2 测试失败

javascript - Angular 5 在模板驱动的表单输入上设置默认值

javascript - 错误 : Member 'feedbackFormDirective' implicitly has an 'any' type in angular

angular - 如何解决在 Angular 6 中为业力测试抛出的 [object ErrorEvent]

angular - 如何制作只有 ng-container 的 if-else Angular 模板?

angular - ChangeDetectionStrategy.OnPush 打破了 ControlValueAccessor 的禁用状态

angular - ngIf 和 formcontrol.touched 不工作

angular - ngrx 和 ngxs 之间的性能差异?