Angularjs:更改$routeChangeError上的路由

Angularjs:更改$routeChangeError上的路由,angularjs,angular-routing,Angularjs,Angular Routing,发生错误时,我正在尝试将路由更改为默认(否则)路由。否则路由设置为显示404错误页面。有人知道怎么做吗?url应该保持原样,这就是为什么我不能使用重定向到/404 角度版本:1.6.2 到目前为止,我得到的是: // inherit function is copied from angular-route.js function inherit(parent, extra) { return angular.extend(Object.create(parent), extra); }

发生错误时,我正在尝试将路由更改为默认(否则)路由。否则路由设置为显示404错误页面。有人知道怎么做吗?url应该保持原样,这就是为什么我不能使用重定向到/404

角度版本:1.6.2

到目前为止,我得到的是:

// inherit function is copied from angular-route.js
function inherit(parent, extra) {
    return angular.extend(Object.create(parent), extra);
}
app.config(['$routeProvider', '$locationProvider', function($routeProvider, locationProvider) {
    $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix('!');

    $routeProvider
        .when('index/:param1/:param2?', {
            controller: 'IndexController',
            templateUrl: 'Resources/Private/JavaScript/Templates/View/Index.html',
            resolve: {
                ajaxData: ['$q', function ($q) {
                    var deferred = $q.defer();
                    //...
                    //on error following reject is called
                    deferred.reject({
                        status: 404
                    });
                    //...
                }]
            }
        })
        .otherwise({
            controller: '404Controller',
            templateUrl: 'Resources/Private/JavaScript/Templates/View/404.html'
        });
}]);

app.run(['$rootScope', '$route', 'HttpError', function ($rootScope, $route, HttpError) {
    $rootScope.$on('$routeChangeError', function (evt,current,previous,rejection) {
        if (rejection.hasOwnProperty('status') && rejection.status === HttpError.NOT_FOUND) {
            var last = $route.current,
                errorRoute = $route.routes[null],
                errorRouteInstance = inherit(
                    errorRoute,
                    {
                        params: {},
                        pathParams: {}
                    }
                );

            errorRouteInstance.$$route = errorRoute;
            errorRouteInstance.loadedTemplateUrl = errorRoute.templateUrl;

            $route.current = errorRouteInstance;
            $rootScope.$broadcast('$routeChangeSuccess', errorRouteInstance, last);
        } else {
            //OR DO SOMETHING ELSE
        }
    });
}]);

未显示错误,也未加载404控制器。

您需要更改url,或者使用嵌套视图。但即便如此,我也不相信
ngRoute
的先进性足以在不更改url的情况下处理此类路由,不像@AlekseySolovey,谢谢你的评论。它还没有找到任何好的解决方案,所以我只使用了一个简单的ng if。如果数据未加载,我将显示404页内容。