Angular UI Router新视图,相同的URL

Angular UI Router new view, same URL

本文关键字:URL UI Router 新视图 Angular      更新时间:2023-09-26

我在我的Angular应用程序中定义了以下状态,我想知道是否有可能从另一个状态注入模板到当前状态?

在下面的.catch()中,我有:$state.go('error404');,它将触发error404状态并将用户重定向到/404。然而,我希望发生的是,用户停留在触发404的相同URL上,404模板只是注入到页面上。(所以没有重定向)。这可能吗?

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

在这种情况下,您应该删除url属性:

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