0

I am trying to navigate to a newly created product after i click submit button. Now so far everything works fine but in my .ts file i want to redirect to lets say edit component of that product after it been created. How can i get the id of newly created component? So far in my ts i have this which redirect to dashboard as soon as product is created, but i want to redirect it to edit that product

const newProduct = { ...product, price: +product.price };
      this.adminService.createProduct(newProduct).subscribe((response: any) => {
        this.router.navigate(['/dashboard/']);
        this.toastr.success('Product Created Successfully');
      });

1 Answers1

0

The createProduct method of adminService should return the ID of the new product, or more precise, it should return an observable that emits that new product ID. What does createProduct exactly do ?

Your code will work with this mock implementation of createProduct:

  createProduct(newProduct): Observable<number> {
    return of(123456);
  }

Your subscription handler will get value 123456 assigned to response enabling you to navigate accordingly.

mcoomans
  • 116
  • 4