フォームにusing this articleというバリデーションを追加しようとしています。 FormBuilder
でコンストラクタを使用すると、EXCEPTION: Uncaught (in promise): Error: DI Error
というエラーがスローされます。FormBuilderがコンソールエラーを返す
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;
}
}
私はこれをどのように修正すればよいのですか?編集1、
を
は私がplunker with an example from angular docsを入れてみましたが、そのいくつかの他のエラーを配っ:
をAJT_82によって示唆されるように、私がconstructor(private fb: FormBuilder){
に私のコンストラクタを変更しました。単純に
constructor(@Inject(FormBuilder) fb: FormBuilder)
:しかしそのは今からコンストラクタでFormBuilder
を注入しようとしている方法を変更するには、
docs https://angular.io/docs/ts/latest/api/forms/index/FormBuilder-class.html – Alex
@ AJT_82なぜそれがそのはに示す方法のように動作しませんあなたのコンストラクタを変更 –
どのバージョンのAngularを使用していますか?おそらくReactiveFormsModule(とFormsModule)をインポートする必要があるためです。 – Alex