2013-05-28 30 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'); 
})