错误TypeError:无法读取formcontrolname中未定义错误的属性“ get”

时间:2019-06-05 13:05:57

标签: angular typescript

我想让表单正确知道,但是我总是收到错误消息

  

“错误TypeError:无法读取未定义的属性”获取”

尽管我尝试了一些操作,例如在formControlName中编写[]或试图通过form.controls获取值,或者没有使这些方法起作用,但是有人可以帮助我吗?我在输入字段中收到错误。

TS文件:

@Component({
  selector: 'chronos-employee-insurrance',
  templateUrl: './employee-insurrance.component.html',
  styleUrls: ['./employee-insurrance.component.css']
})
export class EmployeeInsurranceComponent implements OnInit {
  @Input()
  public employeeId: number;
  @Input()
  public modalRef: BsModalRef;

  isSaving = false;
  private eventManager: JhiEventManager;




  @Input()
  employeeInsurranceForm: FormGroup;

  constructor(
    private fb: FormBuilder,
    private employeeInsuranceService: EmployeeInsurranceService

  ) {

  }

  ngOnInit() {
    this.employeeInsuranceService.findByEmployeeId(this.employeeId).subscribe(res => {

      this.initializeForm(res.body.body);

    });
  }




  initializeForm(insurance: IEmployeeInsurrance) {
    this.employeeInsurranceForm = this.fb.group({
      AFM :       new FormControl(''),
      IKA:       new FormControl(''),
      AMKA:       new FormControl(''),
      SteuerId:   new FormControl(''),
      employeeId: new FormControl(this.employeeId)
    });
    if (insurance) {
      this.employeeInsurranceForm.patchValue(<{ [key: string]: any }>insurance);
    }
  }


  save() {
    this.isSaving = true;
    this.employeeInsuranceService
      .update(this.employeeInsurranceForm.value)
      .subscribe(
        (res: HttpResponse<IEmployeeInsurrance>) => this.onEmployeeInsuranceSaveSuccess(res.body),
      );
  }

  private onEmployeeInsuranceSaveSuccess(result: IEmployeeInsurrance) {
    this.eventManager.broadcast({ name: 'employee-insurance-changed', result });
    this.isSaving = false;
    this.closeModal();
  }

  closeModal() {
    this.modalRef.hide();
  }

HTML文件:

<div class="modal-box">
  <div class="modal-header">
    <button
      aria-label="Close"
      class="close"
      type="button"
      (click)="closeModal()"
    >
      <span aria-hidden="true">×</span>
    </button>
    <h4 class="modal-title">{{ 'Insurrance information' | translate }}</h4>
  </div>

  <form
    >
    <div class="modal-body">
      <div class="master">
          <div class="smart-form" [formGroup]="employeeInsurranceForm" (submit)="save()">
            <fieldset>
                  <section class="col col-6">
                    <label class="input">
                      <i class="icon-prepend fa fa-user fa-fw"></i>
                      <input
                        class="form-control"
                        name="IKA"
                        formControlName="IKA"
                        placeholder="IKA"
                      />

                    </label>
                  </section>

                  <section class="col col-6">
                    <label class="input">
                      <i class="icon-prepend fa fa-user fa-fw"></i>
                      <input
                        type="text"
                        class="form-control"
                        name="AMKA"
                        formControlName="AMKA"
                        placeholder="AMKA"
                      />

                    </label>
                  </section>

                  <section class="col col-6">
                    <label class="input">
                      <i class="icon-prepend fa fa-user fa-fw"></i>
                      <input
                        type="text"
                        name="AFM"
                        class="form-control"
                        formControlName="AFM"
                        placeholder="AFM"
                      />

                    </label>
                  </section>

                  <section class="col col-6">
                    <label class="input">
                      <i class="icon-prepend fa fa-user fa-fw"></i>
                      <input
                        type="text"
                        name="SteuerID"
                        class="form-control"
                        formControlName="SteuerID"
                        placeholder="Steuer ID"
                      />

                    </label>
                  </section>
            </fieldset>
              </div>
          </div>
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" (click)="closeModal()">
        {{ 'Cancel' | translate }}
      </button>
      <button
        type="button"
        class="btn btn-primary"
        (click)="save()"
      >
        <div [ngClass]="{'editableform-loading' : isSaving }"></div>
        {{ 'Save' | translate }}
      </button>
    </div>
  </form>
</div>

2 个答案:

答案 0 :(得分:0)

由于您的表单依赖于异步HTTP调用,因此只需保护它免受错误:

<div *ngIf="employeeInsurranceForm" 
  class="smart-form" 
  [formGroup]="employeeInsurranceForm" 
  (submit)="save()">

并且,正如我在评论中所述,请勿将表单作为输入传递给控件,​​因为您根本不使用它,而是在组件初始化时分配它。

答案 1 :(得分:0)

您可以尝试在构造函数中使用FormBuilder构建表单,然后仅在ngOnInit()中初始化数据。