leaksterrr
leaksterrr

Reputation: 4166

Angular UI Router new view, same URL

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
                })
            }]
        }
    });

Upvotes: 2

Views: 112

Answers (1)

Mik378
Mik378

Reputation: 22191

You should remove the url attribute in this case:

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

Upvotes: 2

Related Questions