Background
On page load I am making a http get call to obtain some data.
There is a form on the page which submits new data to the APi. The response does not hold the new updated data but the data exist in the database after the form is submitted.
I need to access the data by making a second api call after the form is submitted.
Question
After the form is submitted, how do I make the initial API call again to retrieve current data and then update my view component with it?
Code
On page load I call this function inside
Initial call to retrieve data
ngOnInit() { this.getPaymentMethods() }
Function definition
private getPaymentMethods() {
let params: URLSearchParams = new URLSearchParams();
params.set('api_key', this.apiKeyData);
params.set('email', this.auth.user.email);
return this.http.get(STATICS.API_BASE + STATICS.API_ALL_PAYMENT_METHODS,
{ search: params })
.map((res: Response) => res.json()).subscribe((res) => {
localStorage.setItem('payment_profiles', JSON.stringify(res.payment_profiles));
});
}
Handling Response From Initial Call
Parsing the data object response
this.PaymentMethodKeys = Object.keys(this.pMethodsData);
Inside the view I display the data like this
<div *ngFor="let key of PaymentMethodKeys">
{{pMethodsData[key].card_no}}
{{pMethodsData[key].payment_profile_id}}
</div>
This is the form that is submitted to add new data to database
saveCreditCard(model: Payment, isValid: boolean) {
let params: URLSearchParams = new URLSearchParams();
params.set('api_key', this.apiKeyData);
params.set('email', model.email);
params.set('u_address', model.street_address);
params.set('u_city', model.city_address);
return this.http.get(STATICS.API_BASE + STATICS.API_PAY_METHOD,
{ search: params })
.map((res: Response) => res.json())
.subscribe((res) => {
console.log(res);
});
}
}
I think part of my problem is that I am getting the keys from the object inside the constructor. So I think I keep trying to make this work but the only way it will update the keys is when the component refreshes so the constructor will run again.
Any help would be appreciated.
PaymentMethodKeysarray. Second, you return new PaymentMethodKeys array with fresh data. (or 3, router.navigate() to reconstruct the component ?)