我有以下角度6的示例:
//our root app component
import { Component, NgModule, VERSION, Injectable } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule, ReactiveFormsModule, FormArray, FormBuilder } from '@angular/forms';
@Component({
selector: 'my-app',
template: `<form [formGroup]="form" novalidate="">
<div style="margin-bottom:80px">
<div *ngFor="let item of form.controls; let idx=index" [formGroupName]="idx">
<div *ngIf='item.controls.type.value == "control"'>
<div>Input</div>
<input name="answer" type="text" placeholder="Reponse" formControlName="answer"/>
</div>
<div *ngIf='item.controls.type.value == "group"' formGroupName="answer">
<div>Checkboxes</div>
<div *ngFor='let ctrl of item.controls.answer.controls; let index=index'>
<input name="answer" type="checkbox" placeholder="Reponse" [formControlName]="index"/>
</div>
</div>
</div>
</div>
<button type="button" (click)="changeTemplate()">New template</button>
</form>`
})
export class App {
private form:FormArray;
constructor(private formBuilder:FormBuilder){
}
changeTemplate(){
this.form = this.formBuilder.array([
this.formBuilder.group({
answer:[''],
type:['control']
}),
this.formBuilder.group({
answer:this.formBuilder.array([[''], [''], ['']]),
type:['group']
}),
this.formBuilder.group({
answer:[''],
type:['control']
})
]);
}
ngOnInit(){
this.form = this.formBuilder.array([
this.formBuilder.group({
answer:this.formBuilder.array([[''], [''], ['']]),
type:['group']
}),
this.formBuilder.group({
answer:[''],
type:['control']
}),
this.formBuilder.group({
answer:this.formBuilder.array([[''], [''], ['']]),
type:['group']
})
]);
}
}
@NgModule({
imports: [BrowserModule, ReactiveFormsModule],
declarations: [App],
bootstrap: [App],
})
export class AppModule {}
在plunker上出现以下错误:
Can't resolve all parameters for App: (?).
这是the人:http://next.plnkr.co/edit/hmUZdqTR8hpxHj0w?open=lib%2Fapp.ts&deferRun=1&preview
有人知道如何解决此问题吗?
答案 0 :(得分:1)
在App类的构造函数中添加@Inject
装饰器
export class App {
private form:FormArray;
constructor(@Inject(FormBuilder) private formBuilder: FormBuilder){
}
....
}