Angular6-FormControlName-TypeError:无法读取属性';获取';未定义的

Angular6-FormControlName-TypeError:无法读取属性';获取';未定义的,angular,angular-forms,angular-formbuilder,Angular,Angular Forms,Angular Formbuilder,我的项目中有loginForm 但我在“formControlName”中得到一个错误: 错误类型错误:无法读取在处未定义的属性“get” Object.eval[as updateDirectives](LoginComponent.html:7)位于 Object.debugUpdateDirectives[作为updateDirectives](core.js:23910)位于 callViewAction(core.js:23547)上的checkAndUpdateView(core.j

我的项目中有loginForm

但我在“formControlName”中得到一个错误:

错误类型错误:无法读取在处未定义的属性“get” Object.eval[as updateDirectives](LoginComponent.html:7)位于 Object.debugUpdateDirectives[作为updateDirectives](core.js:23910)位于 callViewAction(core.js:23547)上的checkAndUpdateView(core.js:23306) 在checkAndUpdateView的execComponentViewsAction(core.js:23489)中 (core.js:23312)在callViewAction(core.js:23547)在 在checkAndUpdateView中执行EmbeddedViewsAction(core.js:23510) (core.js:23307)位于callViewAction(core.js:23547)

我的登录组件:

  initForm() {
    this.loginForm = this.fb.group({
    email: ['', Validators.required ],
    password: ['', Validators.required]     
   });
    }
我不明白错误出现在哪里

我的输入字段组件


我遗漏了什么?

您可以编写一个小函数,以更简洁的方式返回控件,这样在html中就可以得到下面给出的示例。该错误与您试图访问控件的方式有关

<div class="alert alert-danger" *ngIf="email.dirty">
这将允许您访问相应表单组的控件。您可以为要访问的每个控件编写一个。您也可以返回表单,然后只访问您想要的控件。这将停止stackblitz中的错误,并在屏幕上的
JSON
中显示结果。如果您有任何问题,请发表评论

这样做的一个好处是,在控制器和模板中,您可以使用它执行快速检查

if (this.email.valid)
{
    // any logic where you require a specific form control to be valid.
}

.

这是你的表格

this.loginForm = this.fb.group({
      email: ['', Validators.required ],
      password: ['', Validators.required]     
    });
和HTML格式 您错过了表单名称


电子邮件要求
需要密码

我直接扫了一眼控件名称有误,哈哈:-)好眼力。是的,因为我很想看到代码正常时报告的错误,而且,您推荐的属性将始终确保此问题不再发生
if (this.email.valid)
{
    // any logic where you require a specific form control to be valid.
}
this.loginForm = this.fb.group({
      email: ['', Validators.required ],
      password: ['', Validators.required]     
    });