2013-05-28 27 views

回答

117

您只能使用$stateProvider

你需要注入$urlRouterProvider,并创建类似代码:

$urlRouterProvider.otherwise('/otherwise'); 

/otherwise url必须的状态像往常一样被定义为:

$stateProvider 
    .state("otherwise", { url : '/otherwise'...}) 

看到这个链接,ksperling explains

+0

我试过了,它工作!因此这是最好的答案;) – Adelin

+0

你实际上可以使用'$ stateProvider'。如果您只想显示模板,但不重定向,则工作量会减少。我更喜欢@ juan-hernandez的回答。 – weltschmerz

+0

传递给'否则'的值(在这种情况下''/ otherwise''')必须匹配状态的名称(第一个参数到'.state')或'url'选项的值? – d512

3

好的,当你意识到你问的问题已经在示例代码的第一行中得到了回答的时候,这个愚蠢的时刻!看看示例代码。

 angular.module('sample', ['ui.compat']) 
     .config(
     [  '$stateProvider', '$routeProvider', '$urlRouterProvider', 
     function ($stateProvider, $routeProvider, $urlRouterProvider) { 
      $urlRouterProvider 
      .when('/c?id', '/contacts/:id') 
      .otherwise('/'); // <-- This is what I was looking for ! 


      .... 

Take a look here.

79

您可以使用$stateProvider捕获所有的语法"*path")。你只需要在列表的底部添加的状态配置类似以下:

$stateProvider.state("otherwise", { 
    url: "*path", 
    templateUrl: "views/error-not-found.html" 
}); 

所有的选项都在https://github.com/angular-ui/ui-router/wiki/URL-Routing#regex-parameters解释。

$urlRouterProvider.otherwise(...)相比,此选项的好处在于您不必强制改变位置。

+0

请你详细说明一下:“你不是被迫改变位置”吗? –

+1

他的意思是网址将保持原样。因此,如果用户转到“/ this/does/not/exist”,那么URL将保持在地址栏中。另一种解决方案将带你到'/否' –

+0

我用你的解决方案,它的工作(我可以保持没有找到的网址,这是伟大的,因为我使用http://luisfarzati.github.io/angulartics/和这种方式,我也可以看到导航到网页没有找到)的情况下,用户浏览的网址不存在。但是,如果我在控制器内部使用$ state.go('otherwise')导航到此URL,则会丢失该URL。当用户转到项目的详细信息页面并且服务器返回404时(例如,如果该项目被删除),我明确导航到此状态。你知道解决这个问题的方法吗? – pcatre

31

您也可以手动将$ state插入到其他函数中,然后您可以导航到非url状态。这有利于浏览器不更改地址栏,这有助于处理回到上一页。

$urlRouterProvider.otherwise(function ($injector, $location) { 
     var $state = $injector.get('$state'); 

     $state.go('defaultLayout.error', { 
      title: "Page not found", 
      message: 'Could not find a state associated with url "'+$location.$$url+'"' 
     }); 
    }); 
+0

解决了我的问题,如何检查我们是否中间应用程序或onload否则使用 - 非常感谢你:) –

+0

你救了我天 ! – Titmael

+0

不要忘记,如果你想尽量减少这个你需要$注入 –

0

accepted answer引用angular-route询问ui-router。该接受的答案使用"monolithic"$routeProvider,这就要求ngRoute模块(而ui-router需要ui.router模块)

highest-rated answer使用$stateProvider代替,并说像.state("otherwise", { url : '/otherwise'... }), 但我找不到任何提及“否则”在documentation it links。但是,我看到它说,$stateProvider提到in this answer

你不仅可以使用$stateProvider。您需要注入$urlRouterProvider

这就是我的答案可能对您有帮助的地方。对我来说,这是足够使用$urlRouterProvider就像这样:

my_module 
    .config([ 
    , '$urlRouterProvider' 
    , function(
     , $urlRouterProvider){ 
      //When the url is empty; i.e. what I consider to be "the default" 
      //Then send the user to whatever state is served at the URL '/starting' 
      //(It could say '/default' or any other path you want) 
      $urlRouterProvider 
        .when('', '/starting'); 
        //... 
    }]); 

我的建议是与ui-routerdocumentation,(this specific revision),它包括一个类似用途的一致。when(...)方法(第一次调用函数):

app.config(function($urlRouterProvider){ 
    // when there is an empty route, redirect to /index 
    $urlRouterProvider.when('', '/index'); 

    // You can also use regex for the match parameter 
    $urlRouterProvider.when(/aspx/i, '/index'); 
})