5

We're using angular-ui-router (version 0.2.10 i believe).

There are two primary ways to arrive at a state,

  • a) a user hits / ammends the url which corresponds to a state
  • b) code somewhere e.g. controller invokes $state.go().

We would like in case a) to validate the state (e.g. is it valid according to one or more business rules), but not to do so in case b) since we're pretty sure that our application will only transition to valid states.

Case a) will often require an http round trip to perform validation which we'd like to avoid if possible.

How would this be implemented?

Thanks!

4
  • 1
    Why not use resolve to check certain criteria before loading the page? You can then pass data into the controller. See Resolve on UI-Router Commented Aug 19, 2014 at 13:18
  • @Josh, I was aware of resolve and had considered it. I would need to pass custom data to the stateProvider to determine the difference between a) and b) above, and then check out the legitimacy of being able to redirect if the state validation fails at this point? Commented Aug 19, 2014 at 20:33
  • 1
    hmm. I'm wondering if you can pass a $stateParam not in the url, as an object, that you can process in the resolve. See params subsection here, and processing params in resolve. That should differentiate between state.go() and typing in the url. If that doesn't work, not sure Commented Aug 19, 2014 at 21:22
  • Hmm, thanks Josh. We've taken a different approach for now in that we've stopped exposing the url's (location:false) and "abstracted" them, so users will have difficulty arriving at states via the url. Not 100% the best but we have many other issues to solve! Thanks for your input. Commented Aug 22, 2014 at 0:16

1 Answer 1

0

You could wrap the validation in a service and call it every time the state changes. However, before moving to another state with $state.go(), you instruct the validation service to consider the particular parameters you are passing as valid. E.g.

validationService.trustAsValid(yourData);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.