我有一个嵌套表单,这里是formcontrols:
this.newRequest = this._fb.group({
requestType: [],
tripType: [],
feeders: [''],
directFlight: [],
departure: [''],
arrival: [''],
depDate: [''],
arrDate: [''],
nbPax: ['', [Validators.max(95), Validators.min(1), Validators.pattern('^[0-9]+$')]],
class: [''],
daterangeToggle: [],
viaToggle: [],
detailsToggle: [],
roundway: this._fb.array([
this.initRoundway()
])
});
initRoundway() {
return this._fb.group({
daterangeOutFrom: [],
daterangeOutTo: [],
viaOut: this._fb.array([]),
viaIn: this._fb.array([]),
flights: this._fb.array([
this.initFlights()
])
});
}
initFlights() {
return this._fb.group({
class: ['', Validators.required],
flightNb: [''],
timeMin: [''],
timeMax : ['']
});
}
我在访问viaOut
和flightNb时遇到问题。
我试过了:
const control1 = <FormArray>this.newRequest.controls['roundway'];
if (control1.controls['viaOut'].length===0) {...}
还有:
const control1 = <FormArray>this.newRequest.get('roundway.viaOut');
if (control1.length === 0) {...}
但两者都不起作用,我收到错误"Cannot read property 'length' of null"
修改
这是使用第一个代码的console.log(control1)的输出。
感谢。
答案 0 :(得分:0)
嗨,请试试这个,
const control1 = <FormArray>this.newRequest.controls['roundway'];
if (control1.controls['viaOut']['controls'].length===0) {...}
OR
const control1 = <FormArray>this.newRequest.get('roundway.0.viaOut');
if (control1.length === 0) {...}