【发布时间】:2017-04-05 11:54:31
【问题描述】:
我正在尝试向我的表单 using this article 添加验证。
当我将构造函数与FormBuilder 一起使用时,它会抛出错误,指出EXCEPTION: Uncaught (in promise): Error: DI Error。
test1.component.ts
import { Component, Inject, OnInit } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
import { FormComponent } from '../form/form.component';
@Component({
selector: 'app-test1',
templateUrl: './test1.component.html',
styleUrls: ['./test1.component.css']
})
export class Test1Component extends FormComponent {
complexForm : FormGroup;
constructor(@Inject(FormBuilder) fb: FormBuilder){
super();
this.complexForm = fb.group({
'firstName' : "",
'lastName': "",
'gender' : "Female",
'hiking' : false,
'running' : false,
'swimming' : false
});
}
saveForm(){
console.log("Child Form save");
return true;
}
}
我该如何解决这个问题?
我试图提出一个plunker with an example from angular docs,但它给出了一些其他错误,
编辑 1:
按照 AJT_82 的建议,我将构造函数更改为 constructor(private fb: FormBuilder){。然而它现在给了我另一个错误
【问题讨论】:
-
改变你的构造函数...plnkr.co/edit/tEoVU9enXRGWOClJWVQT?p=preview
-
@AJT_82 为什么它不像文档中显示的那样工作 angular.io/docs/ts/latest/api/forms/index/…
-
您使用的是哪个版本的 Angular?您遇到的第二个错误是因为您很可能需要导入 ReactiveFormsModule(和 FormsModule)。
-
ng version说@angular/core: 2.4.10 -
好的,所以我在 plunker plnkr.co/edit/6T3NVkrMhfm5TrCnJIxO?p=preview 上尝试了同样的方法,它可以工作,但不能在我的机器上工作。您认为这是与此版本相关的错误吗?
标签: angular