When retrieving an object from a backend API service like this:
data: {firstName:'pepe',lastName:'test', address = {street: 'Cervantes', city:'Villajoyosa'} }
or data: {firstName:'pepe',lastName:'test', address = null }
I am facing an issue where I need to set the object in the formGroup. However, when the address is null, I want to save it as null instead of {street: '', city:''}}
form = this.fb.group({
firstName: [''],
lastName: [''],
address: this.fb.group({
street: [''],
city: [''],
}),
});
// setting the form:
if(data.address)
this.form.controls['address'].setValue(data.address)
save(){
valueToSend = this.form.controls['address'].getValue()
}
// template
<button (click)=save()>Save</button>
<div formGroupName="address">
<h3>Address</h3>
<label>
Street:
<input type="text" formControlName="street">
</label>
<label>
City:
<input type="text" formControlName="city">
</label>
</div>