【发布时间】:2019-11-15 06:17:14
【问题描述】:
我尝试了 Angular Reactive 表单和我的代码 组件类为:Component Class file
而我的 HTML 代码是:HTML file
我在控制台中收到此错误:
core.js:15724 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'controls' of undefined
TypeError: Cannot read property 'controls' of undefined
at new LoginComponent (login.component.ts:24)
我知道这个错误是因为这条线:
validEmail = {
field: true,
error: this.myForm.controls.email.invalid && this.myForm.controls.email.dirty
};
另外,如果我尝试评论和“错误:”ngClass 对象, 条件:
!myForm.invalid
在登录表单中提交按钮不起作用。
我是 Angular 的新手,文档比较笼统。所以请帮助我如何解决它。 对于您的测试,请使用语义 UI 库来查看 HTML。
【问题讨论】:
-
可以把代码以文字的形式贴出来吗?
-
为了我的问题质量,我把它放在 Pastebin 中。
-
@Sivaramakrishnan,你总是在 ngOnInit 中创建一个表单,写成
<form *ngIf="myForm" [formGroup]="myForm"...>这样的表单很有用。这些 *ngIf 避免了初始错误
标签: angular angular-reactive-forms