Render 404 instead of redirecting 404 in angularjs

后端 未结 3 987
旧巷少年郎
旧巷少年郎 2021-02-13 05:31

I\'m looking for a good approach to render 404 page instead of redirecting 404 page in angularjs. Many solutions I have found is all about redirecting to another page. It will c

3条回答
  •  时光取名叫无心
    2021-02-13 06:14

    I'm new to AngularJS, so this may not be an ideal solution, but it works for showing a 404 page, or similar uses such as a login page:

    See Working Example

    Redirect everything to the same master template:

    $routeProvider
    .when('/', {
            controller: 'homeController',
            templateUrl: 'partial.master.html'
        })
    .when('/cust/:custid', {
            controller: 'custController',
            templateUrl: 'partial.master.html'
        })
    

    master.html template refers to the masterController and has a subpage:

    {{title}} - Header



    {{title}} - Footer

    masterController has conditional sub-page logic:

    controllers.custController = function($scope, $rootScope, $routeParams){
        $rootScope.subpage = 'cust';
        $scope.cust = getCustomer( $routeParams.custid );
    };
    controllers.masterController = function($scope, $rootScope) {
        switch($rootScope.subpage) {
        case 'home':
            $scope.subPage = 'partial.home.html';
            break;
        case 'cust':
            if($scope.cust) {
                $scope.subPage = 'partial.cust.html';
            } else {
                $scope.subPage = 'partial.404notfound.html';
            }
            break;
    

提交回复
热议问题