2

I have the following state defined in my Angular app and I'm wondering if it's possible to inject a template from another state into the current state?

In my .catch() below I have: $state.go('error404'); which will trigger the error404 state and redirect the user to /404. However what I'd like to happen is that the user stays on the same URL that triggered the 404 and the 404 template is just injected on to the page. (so no redirect). Is this possible?

.state('error404', {
        url: '/404',
        template: '<p style="color:white;">404 NOT FOUND</p>'
    })

.state('publicUserProfile', {
        url: '/:username',
        templateUrl: 'views/public-profile.html',
        controller: 'publicProfileController',
        resolve: {
            userData: ['$http', 'API_URL', '$stateParams', '$state', function ($http, API_URL, $stateParams, $state) {
                return $http
                .get(API_URL + '/users/' + $stateParams.username)
                .catch(function () {
                    $state.go('error404'); <-- HERE
                })
            }]
        }
    });

1 Answer 1

2

You should remove the url attribute in this case:

.state('error404', {
        template: '<p style="color:white;">404 NOT FOUND</p>'
    })
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, how did I miss that? Thanks!

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.