Mohammed Ajmal
Mohammed Ajmal

Reputation: 590

Router.navigate doesn't work in angular app

I have the following code to perform http PUT:

updateProduct(form: any) {
  this.productService.updateProduct(form, this.id).subscribe(
    (data: any) => data
  );
  this.route.navigate(['']);
}

service.ts:

 updateProducts(productForm, id) {


  const temp = {
        'description': productForm.description,
        'quality': productForm.quality
    };
    return this.httpObj.put(`${this.uri}/products/${id}`, JSON.stringify(temp), {
        headers: new HttpHeaders({
            'Content-Type': 'application/json'
        })
});
}

The problem is that I am able to perform the update operation but the submit button doesn't route to the home route.

Upvotes: 0

Views: 63

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222522

You need to place the router.navigate within the subscribe,

updateProduct(form: any) {
  this.productService.updateProduct(form, this.id).subscribe((data: any) => (
     this.route.navigate(['']);
 )};
};

Upvotes: 1

Related Questions