如何获取带有数据的部分并避免angular2中的空白部分

how to fetch the section with data and avoid empty section in angular2

我有一个部分,数据即将到来,因为我已经提供了静态数据。 但是除了静态数据,我也得到了空白部分。 所以,谁能帮我只获取数据并避免空集。

TS:

this.Emergencies = [{ 
  "ContactName": "Person1", 
  "Phone": "12345678", 
  "Relationship": "1" 
  "ContactName": "Person2", 
  "Phone": "37438367", 
  "Relationship": "4" 
  }]
this.emergencyContactForm = this._fb.group({
  itemRows: this._fb.array([this.createEmergency()])
});
this.Emergencies.forEach( 
  emergency => { 
  const control = <FormArray>this.emergencyContactForm.get('itemRows')['controls']; 
  control.push(this.createEmergency()); 
  }) 
  console.log(this.emergencyContactForm.get('itemRows')['controls'])
  for (let entry in this.Emergencies) { 
    this.emergencyContactForm.get('itemRows')['controls'][entry].patchValue({ 
    ContactName: this.Emergencies[entry].ContactName, 
    Phone: this.Emergencies[entry].Phone, 
    Relationship: this.Emergencies[entry].Relationship, 
    })
  }  }, 
  }

而且,如果没有数据,我希望显示空白部分,如果有数据,则必须只显示数据

由于您对所有 emergencies 使用 control.push(this.createItem());,因此在创建 control

时无需使用 [this.createEmergency()]

您有:

this.Emergencies.forEach(
emergency => {
  const control = <FormArray>this.emergencyContactForm.get('itemRows')['controls'];
  control.push(this.createItem());
})

改变,

this.emergencyContactForm = this._fb.group({ 
    itemRows: this._fb.array([this.createEmergency()]) 
});

到,

this.emergencyContactForm = this._fb.group({ 
    itemRows: this._fb.array() 
});

所以,现在你不会得到额外的formControl

更新:

if(this.Emergencies.length == 0){
  const control = <FormArray>this.emergencyContactForm.get('itemRows')
  ['controls'];
      control.push(this.createItem());
}