我在angular2和typescript中构建了一个Form.我尝试动态添加一个复选框列表,这对我不起作用,我的错误在哪里?
模板代码:
<div formArrayName="categories">
<div class="form-group" *ngFor="let category of updateDetailsForm.controls.categories.controls; let i = index">
<label>
<input type="checkbox" formControlName="{?{i}}">
{?{category.name}}
</label>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
Typescript代码:
updateDetailsForm: FormGroup;
private categories : Category[] = []
constructor(private router: Router,
private ss : SearchService,
private formBuilder: FormBuilder)
{
this.initForm() // before the categories loaded
this.ss.getAllCategories().subscribe(
data => {
this.categories = data
this.initForm() // refresh the form with categories
}
)
}
initForm() {
let allCategories: FormArray = new FormArray([]);
for (let i = 0; i < …Run Code Online (Sandbox Code Playgroud)