0

I have a senario that has been causing me issues for the last few weeks now. I currently have a "homepage" that populates with data from a controller with sports stats. This data comes from a service that is also used for the individuals pages for this sports stats.

For instance a user is able to click one of the listings on the home page and get a detailed list of that particualar entry with a state change from sports.com to sports.com/id/sport

I do this by taking the id from the home service and pass that through the paramenters within the state for the details page. From here I use that same service with the @id as a paramenter in order to get the details for that page (using $stateParams.id).

Normally that would work fine, but here is the problem. Sometimes when hitting the details page the service fires off the get request before the $stateParams.id is availibe and I end up with an error in my request. So instead of /json.php?detail=id im getting /json.php?detail=

For a cheap fix I now have the search query waiting on a timeout for 800ms in order to give the state time to resolve the $stateParams.id and then finally send out the request. My question is, what is a better way to do this? Is this something experienced often? It seems like in all my time with angular I haven't run into this situation so I'm a bit at a lost. Thanks

Mr. BigglesWorth
  • 1,530
  • 3
  • 18
  • 33
  • I have not heard of this case, `$stateParams` is set before the state is load and the controller ran. Its even set before and provided in the `$stateChangeSuccess` event that happens ~first https://github.com/angular-ui/ui-router/blob/355d5a1e5ee61a3ad3bcf267584f11729451ae60/src/state.js#L908. Do you have any code to show to help debug? – TheSharpieOne Sep 17 '14 at 22:29
  • I'll try to get some code in a plunkr, but its all off a locked down REST enviroment atm. This does seem similar to my promblem [link to issue](http://stackoverflow.com/questions/21452537/ui-router-use-stateparams-in-service) – Mr. BigglesWorth Sep 17 '14 at 22:50
  • The problem with the other issue you linked is that it is trying to resolve the service and call the function prior to the state being set up. If you are also trying to do thing is a service/factory in the state's resolve, then you will also have this issue. Let me know if this is the case for you. – TheSharpieOne Sep 18 '14 at 14:07

0 Answers0