AngualrJs: how to detect when user enters an invalid state in the brower's address bar?

Issue

Somewhat related : Angular – ui-router get previous state.

I have search results view. Obviously, it makes no sense for the user to enter this URL into the browser’s address bar & navigate there.

I would like to detect if (s)he does so & issue an admonition.

But, how? If a user goes directly there, there is no state transition. So, how do I detect it?

Solution

I settled for a service, which I already use to share data between controllers , when changing state. The reason being that transition state change OnSuccess() does not work the first time, as per

https://github.com/angular-ui/ui-router/issues/3536 and may others

// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=+=+=+=
Self.setState = function (currentState, newState) {
    Self.setPreviousState(currentState);
    console.log("Change state from [" + currentState + "] to [" + newState + "]");
    Self.state = newState;
    $state.go(newState);
}

// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=++=+=+=+=+=+=+=+=+=+=+=
Self.getState = function () {
    console.log("Current state is [" + Self.state) + "]";
    return Self.state;
}

// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Self.getPreviousState = function () {
    return Self.previousState;
}
// +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
Self.setPreviousState = function (previousState) {
    Self.previousState = previousState;
}

Answered By – Mawg says reinstate Monica

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published