无法直接访问 FormControl 实例。无法读取未定义的 属性 'invalid'

Can't acces FormControl instance directly. Cannot read property 'invalid' of undefined

无法像 Angular 文档中那样访问它,所以必须先获取 FormGroup 实例并在其中找到 FormControl 实例。我想知道为什么?这个例子有效:

<form [formGroup]="myForm" (ngSubmit)="onSubmit()">
  <div class="form-group">
    <label for="username">Username</label>
    <input 
      type="text"
      name="username"
      class="form-control"
      formControlName="username"
    >
    <div *ngIf="myForm.controls.username.invalid" class="alert alert-danger">
      username is required
    </div>
  </div>

虽然这会引发错误(它们之间的区别仅在 *ngIf 语句中):

<form [formGroup]="myForm" (ngSubmit)="onSubmit()">
  <div class="form-group">
    <label for="username">Username</label>
    <input 
      type="text"
      name="username"
      class="form-control"
      formControlName="username"
    >
    <div *ngIf="username.invalid" class="alert alert-danger">
      username is required
    </div>
  </div>

Cannot read property 'invalid' of undefined

form.component:

import {Component} from '@angular/core';
import {FormGroup, FormControl, Validators} from '@angular/forms';

@Component({
  selector: 'sign-up',
  templateUrl: 'app/sign-up.component.html'
})

export class SignUpComponent {

  myForm = new FormGroup({
    username: new FormControl('username', Validators.required),
    password: new FormControl('', Validators.required),
  });
}

它会抛出错误,因为您没有名为 usernamepassword.

的变量

为了解决这个问题,您可以:

  1. 将控件存储在组件变量中:

TS:

@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent {
  readonly usernameCtrl = this.formBuilder.control('username', Validators.required);
  readonly passwordCtrl = this.formBuilder.control('', Validators.required);
  readonly formGroup = this.formBuilder.group({
    username: this.usernameCtrl,
    password: this.passwordCtrl
  });

HTML:

<div 
  *ngIf="userNameCtrl.invalid" class="alert alert-danger"
>
  username is required
</div>
  1. 使用AbstractControl#get抢占控制权:

HTML:

<div 
  *ngIf="formGroup.get('username').invalid" class="alert alert-danger"
>
  username is required
</div>
  1. 使用 AbstractControl#hasError 这样您就可以为每个现有的验证指定不同的消息:

HTML:

<div 
  *ngIf="formGroup.hasError('required', 'username')" class="alert alert-danger"
>
  username is required
</div>

DEMO

您可以使用 Form Group 并在您的控制器中定义相应的 getter 来解决此问题。为了实现这个目标:

在控制器中:

1) 去掉表单控件变量的定义和初始化

usernameCtrl: FormControl;
passwordCtrl: FormControl;
...
this.usernameCtrl = this.formBuilder.control('username',Validators.required);
this.passwordCtrl = this.formBuilder.control('', Validators.required);

2)将表单组初始化改成这样

ngOnInit() {
this.myForm = this.formBuilder.group({
  username: ['usename', Validators.required]
  password: ['', Validators.required]
});

}

3) 添加吸气剂

get username() { return this.myForm.get('username'); }
get password() { return this.myForm.get('password'); }

模板中:

1) 使用 [formGroup]="MyForm"

添加父级 div
<div [formGroup]="myForm">
...
</div>

2) 将 [formControl]="usernameCtrl" 更改为 forcontrolName=username 并将 *ngIf="usernameCtrl.invalid" 更改为 *ngIf="username.invalid"

<input type="text"
       name="username"
       class="form-control"
       formControlName="username">
  <div *ngIf="username.invalid" class="alert alert-danger"> 
    username is required
  </div>

3) 将 [formControl]="passwordCtrl" 更改为 forcontrolName=password 并将 *ngIf="passwordCtrl.invalid" 更改为 *ngIf="password.invalid" te.

<input type="text"
       name="password"
       class="form-control"
       formControlName="password">
  <div *ngIf="password.invalid" class="alert alert-danger">
    password is required
  </div>

Plunker

对我来说作品:

form.component:

getFormControl(name) {
    return this.Form.get(name);
}

模板:

<input 
  type="text"
  name="username"
  class="form-control"
  formControlName="username"
>
<div *ngIf="getFormControl('username').invalid" class="alert alert-danger">
  username is required
</div>

我遇到了同样的问题,我将 'this' 添加到“myForm.controls.....”。它帮助了我。

而不是:

<div *ngIf="myForm.controls.username.invalid" class="alert alert-danger">
      username is required
 </div>

做:

 <div *ngIf="this.myForm.controls.username.invalid" class="alert alert-danger">
      username is required
 </div>

希望对您有所帮助。

在ts文件中添加:

get username() { return this.myForm.get('username'); }
get password() { return this.myForm.get('password'); } }

实际上我是 Angular 的新手,我刚用了一个月,也在寻找一些答案嘿嘿但是在你的组件中添加一个 getter 像这样:

export class SignUpComponent {

 myForm = new FormGroup({
    username: new FormControl('', Validators.required),
    password: new FormControl('', Validators.required),
  });

get username(){
  return this.myForm.controls['username'];
}

}