Function running before my request response

Asked

Viewed 70 times

0

In a modal I have a form with data that can be edited. If the user confirms the update the data is updated and then it will be forwarding the new page. But on this new page the information that appears is still the old information (before the update). If I give an F5 the new information appears.

Information before updating inserir a descrição da imagem aqui

New information inserir a descrição da imagem aqui

Information that appears at the first moment when I confirm the update. inserir a descrição da imagem aqui

After F5 inserir a descrição da imagem aqui

Follow the code below:

updateClonedQuote(): void {
    this.canDeactivate = false
    const updateQuote = this.cloneQuoteForm.getRawValue() as ClonedQuoteValue
    this.quoteService.updateQuote(updateQuote, this.dataClonedQuote.id)
      .subscribe(
        () => {},
        (err) => console.log(err),
        () => {
          this.modalService.dismissAll()
          this.route.navigate(['quotes-abertas/', this.dataClonedQuote.id])
        }
      )
  }
  • Add the code of your second page (Quotes-open), it seems that the problem is in it.

  • What happens is: when I open http://localhost:4200/Quotes-open/324 a get request takes the information and fills the fields. When I make a copy I am automatically redirected to this new proposal with url http://localhost:4200/Quotes-open/325, but the functions within ngOnInit() are not run again. I thought the angular performed these functions again, but it seems that no

1 answer

1

Checks that the browser is not "caching" the request to the server. If it is, it will always return the same data, because the browser thinks you are asking for exactly the same information.

If this is the case, you can add a random value at the end of the url for the browser to think that it is always a new request, as for example

// random number between 0 and 999
const version = Math.floor(Math.random() * 1000);
const requestEndpoint = `https://endpoint.com/api/quotes?=v${version}`

This way the request is always difference.

  • I gave an explanation of what happens up there. My Urls are different, but the error persists

  • ngOnInit is only executed when the component is loaded. In this case, as you do not change the component, Ngoninit is not done again. Use ngAfterViewChecked() before. This method is always being looped during the life of the view. Store this Id that you receive per parameter in a local variable, and in this method check that the url id has been changed from the id you have saved. If this condition is true, ask again for information.

  • Do you have a solution for that? ngOnChanges() solves my life?

  • Or you can also see if there are changes to the url by pinging the router this.router.Events.subscribe(r => ...);

  • From what I have seen here ngOnChanges is only used with Inbounds proportys. This is not my case. How do I use this.router.Events.subscribe? It from the Route or Router interface?

  • Is Router "import { Router } from '@angular/router';"

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.