I would like to reset form after submitting. Respectively, my use-case is the following:
1. Fill form (template-driven solution).
2. Get model from form and with service pass it to the database.
2.1. Add model to database
2.2. Assign model to an array
3. Update view by this updated array.
The problem is, that if I reset form after calling my service method, the data have sent to database are valid, but in client I am receiving null values – meaning that on refresh angular client (browser) the data are fully loaded without any error. On the other hand, if reset method is not included, all works fine, as expected – only the form not reset (logically).
I tried debugging my solution, more or less unsuccessfully. However, one thought occurred to me. In that service layer the asynchronous call is made, is it somehow possible that method for resetting form can bias parameter in middle of processing the request, the method addCrop?
Component
Injectable()
export class CropsComponent implements OnInit {
options$ : Observable<StyleModel[]>;
crop$;
documentTypes = ['Pdf','LaTeX'];
//used for binding in form
cropModel : CropModel;
constructor(private _styleService: StyleRepositoryService) { }
ngOnInit() {
this._styleService.getCropOnChange().subscribe(crop => this.cropModel = crop);
this.options$ = this._styleService.getStylesOnChange();
}
onSubmit(f:NgForm)
{
if(f.valid){
//calling service layer for storing this model in database
this._styleService.addCrop(this.cropModel);
//resetting form values
f.reset();
}
}
}
Service
@Injectable()
export class StyleRepositoryService {
/*
[abbrev.]
*/
addCrop(crop: CropModel){
this.dataService.createCrop(crop).subscribe(data =>{
//find style according to CropModel styleId. Style model include array of cropModels
let stl = this.styles.find(p => p.styleId == data.styleId);
//add this cropModel to found style
stl.styleCrops.push(crop);
//log inform me about added model with null values
console.log(this.styles);
//notify other components that styles array have updated.
this.styles$.next(this.styles);
})
}
/*
[abbrev.]
*/
}
Service made request with success - model added to database. As I mentioned at the top. Is it possible that passed parameter crop can be intercept by reset form method even it has been passed? So that, when I am adding the model (parameter from addCrop method) to array model is null.
EDIT
addCrop method runs post request which return data – the back-end server returns same model as the one created in Angular. So, if I use returned data, view is properly changed. So to clarify the question. Can the reset form method change the value of the attribute crop in the addCrop method at half of its run?
addCrop(crop: CropModel){
this.dataService.createCrop(crop).subscribe(data =>{
//find style according to CropModel styleId. Style model include array of cropModels
let stl = this.styles.find(p => p.styleId == data.styleId);
//add this cropModel to found style
//instead using parameter crop, I used returned model from backend, data
stl.styleCrops.push(data);
//log inform me about added model with null values
console.log(this.styles);
//notify other components that styles array have updated.
this.styles$.next(this.styles);
})
}
this.dataService.createCropis an async operation? So thef.reset()is occurring before that operation is complete. Since you are sending out a notification when the operation is complete (this.styles$.next) you could move your reset logic into your subscribe.