我认为这可能是 Reactive Form 的错误。我会感谢更有经验的 Angular 专家的任何帮助。
症状:在给定时间无法在输入中键入超过 1 个字符。
发生:当输入是 FormArray 中的 FormArray 时
我包含了一个 Plunker 链接,如下所示: https ://embed.plnkr.co/zl2BQe/ 。
//app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
ReactiveFormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
//app.component.ts
import { Component, OnInit} from '@angular/core';
import { FormArray, FormControl, FormGroup } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
testForm: FormGroup;
ngOnInit(){
this.testForm = new FormGroup({
'lines': new FormArray([
new FormGroup({
'fields': new FormArray([
new FormControl('')
])
})
])
})
}
onAddLine(){
if(this.testForm.value.lines.length<10){
(<FormArray>this.testForm.get('lines')).push(
new FormGroup({
'fields': new FormArray([
new FormControl()
])
})
)
}
}
onAddField(){
// Not developed
}
}
//app.component.html
<div class="panel panel-default">
<div class="panel-heading">
Angular Reactive FormArray Error
<!--<button class="btn btn-primary" (click)="onAddField()">Add
Field</button>-->
<button class="btn btn-primary" (click)="onAddLine()">Add
Line</button>
</div>
<div class="panel-body">
<form [formGroup]="testForm">
<div class="form-inline" *ngFor="let line of testForm.value.lines; let i = index" formArrayName="lines">
<div class="form-group">
<span [formGroupName]="i">
<span *ngFor="let field of testForm.value.lines[0].fields; let j = index" formArrayName="fields">
<input type="text" class="form-control" [formControlName]="j">
Please Type on Input Field
</span>
</span>
</div>
</div>
</form>
</div>