2

In my AngularJS application I want to catch errors and send the user to a 404 state like so:

$rootScope.$on('$stateNotFound',  function(event, unfoundState, fromState, fromParams){ 
    //console.log(unfoundState.to);
    //console.log(unfoundState.toParams);
    //console.log(unfoundState.options);
    $state.go('404');
});

This is for when a user clicks a bad link (e.g. has no state, as ones with states but no content are handled by the otherwise method) in the app, etc.

The code loads the 404 state fine:

.state('404',
        {
            views: {
                'body': {
                    templateUrl: 'partials/404.html',
                }
            }
        });

But what I want to do is change the URL to the state they tried to access, so basically load the 404 state but use the incorrect URL (as a 404 would work in a server environment).

I've looked into doing:

history.replaceState(null, null, unfoundState.to);
$state.go('404');

But that causes major errors in the app and changes the URL but not the state!

How can I do this?

1
  • 1
    I have not tried. I'm not sure whether $state.transitionTo('404',{},{location:false}); works. Commented Oct 6, 2014 at 14:29

2 Answers 2

1

According to the docs, I think you can do:

$state.go('404',{},{location:false});

Note: I'm in the process of creating a plunkr to verify but I don't have time to finish it up completely right now

Sign up to request clarification or add additional context in comments.

Comments

0

Try this to catch all URLs not matching any of your previous routes :

.state("404", {
    url: "*path", // will catch all URLs
    templateUrl: 'partials/404.html'
})

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.