AngularJS否则路线不工作?

AngularJS否则路线不工作?,angularjs,Angularjs,我的AngularJS重定向如下所示: // create the module and name it asApp var asApp = angular.module('asApp', ['ngRoute']); // configure our routes asApp.config(['$routeProvider', '$httpProvider', '$locationProvider', function($routeProvider, $httpProvider, $locat

我的AngularJS重定向如下所示:

// create the module and name it asApp
var asApp = angular.module('asApp', ['ngRoute']);

// configure our routes
asApp.config(['$routeProvider', '$httpProvider', '$locationProvider', function($routeProvider, $httpProvider, $locationProvider) {
    $routeProvider

    // route for the home page
    .when('/', {
        title: 'Home of PeppyBurro – Radical Spanish learning tips and tricks for the adventurous learner',
        templateUrl : 'pages/home.html',
        controller  : 'mainController'
    })

    // route for the about page
    .when('/about', {
        title: 'About PeppyBurro – Radical Spanish learning tips and tricks for the adventurous learner',
        templateUrl : 'pages/about.html',
        controller  : 'mainController'
    })

    // route for the 404 page not found error
    .when('/notfound', {
        title: 'Page not found',
        templateUrl : 'pages/404.html',
        controller  : 'mainController'
    })

    .otherwise('/notfound', {
        title: 'Page not found',
        templateUrl : 'pages/404.html',
        controller  : 'mainController'
    })
}]);

我需要的是,当URL中输入未定义的slug时,脚本将我路由到404.html。因此,如果用户进入
mysite.com/#/about
,它应该显示
about.html
页面。这个钻头很好用。但是当用户进入,比如说,
mysite.com/#/abc
时,它应该显示我在
中定义的
404.html
页面。否则,
当('/notfound')
指令。但是,尽管
.when('/notfound')
位工作,但
位不工作。我缺少什么吗?

我认为您应该修改为:

 .otherwise({
      redirectTo: '/notfound'
  }

参考

我认为您应该修改为:

 .otherwise({
      redirectTo: '/notfound'
  }

参考

您可以使用
重定向到

.otherwise({
    redirectTo: '/notfound'
});

您可以使用重定向到

.otherwise({
    redirectTo: '/notfound'
});

.时间和.否则是两码事

.何时定义路线 。否则,将在尚未创建路由时设置默认路由。

你有两个选择

  • 拿出路线

    $routeProvider
        // ... other routes
        .otherwise({
            title: 'Page not found',
            templateUrl : 'pages/404.html',
            controller  : 'mainController'
        })
    
  • 或者使用.when定义404路由,并将路由路径传递到。否则

    $routeProvider
            // ... other routes
            .when('/notfound', {
                title: 'Page not found',
                templateUrl : 'pages/404.html',
                controller  : 'mainController'
        })
        .otherwise('/notfound');
    
  • 希望这有帮助! 如果你有任何问题,请告诉我

    编辑

    // As of Angular 1.3
    
    .otherwise('/notfound')
    // is short hand for
    .otherwise({redirectUrl: '/notfound'})
    

    .时间和.否则是两码事

    .何时定义路线 。否则,将在尚未创建路由时设置默认路由。

    你有两个选择

  • 拿出路线

    $routeProvider
        // ... other routes
        .otherwise({
            title: 'Page not found',
            templateUrl : 'pages/404.html',
            controller  : 'mainController'
        })
    
  • 或者使用.when定义404路由,并将路由路径传递到。否则

    $routeProvider
            // ... other routes
            .when('/notfound', {
                title: 'Page not found',
                templateUrl : 'pages/404.html',
                controller  : 'mainController'
        })
        .otherwise('/notfound');
    
  • 希望这有帮助! 如果你有任何问题,请告诉我

    编辑

    // As of Angular 1.3
    
    .otherwise('/notfound')
    // is short hand for
    .otherwise({redirectUrl: '/notfound'})