2015-01-15 89 views
4

我有一個使用ui-router的AngularJS應用程序。所有的工作不錯,但我想有這樣的訪問用戶註冊確認屏幕:如何在沒有首先導航到index.html的情況下直接進入具有ui路由器的狀態

http://localhost:2757/Auth/confirmation 

在我目前的配置,當我打開瀏覽器頁面,這個鏈接看起來不爲index.html,然後做不要進入/ Auth /確認狀態。我明白爲什麼會發生這種情況,但我不知道如何解決問題。

有人可以給我一些建議,如何/如果我可以做一個鏈接,將直接讓我到/認證/確認

所有我能想到的也許是這樣的:

http://localhost:2757/index.html?load=AuthConfirm 

然後以某種方式有一個檢查,檢測到一次的index.html加載了一個新的狀態過渡。

這裏是我的AppConfig文件:

var auth = { 
    name: 'auth', 
    url: '/Auth', 
    views: { 
     'root': { 
      templateUrl: 'app/auth/partials/home.html' 
     } 

    } 
}; 

var authContent = { 
    name: 'auth.content', 
    url: '/:content', 
    views: { 
     'root': { 
      templateUrl: 'app/exams/partials/home.html' 
     }, 
     'content': { 
      templateUrl: function (stateParams) { 
       return 'app/auth/partials/' + stateParams.content + '.html' 
      } 
     } 
    } 
} 

這裏的東西我想:

http://localhost:2757/index.html < <帶我到我的主索引頁沒有找到

http://localhost:2757/index.html/Auth < <返回頁面

http://localhost:2757/index.html/Auth/register < <返回頁面沒有找到

回答

4

我想說的是,我們需要的是 - 讓index.html成爲一個SPA(單頁應用程序)。

.when() for redirection

參數:然後我們會從內置在UI的路由器功能中獲益

什麼字符串| RegExp | UrlMatcher您要重定向的傳入路徑。
處理程序字符串|功能您要將用戶重定向到的路徑。

handler作爲字符串

如果處理機是一個字符串,它是按照匹配(即像與string.replace()爲正則表達式的句法視爲重定向,和被內插,或像UrlMatcher其他模式)。

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'); 
}) 

.otherwise() for invalid routes

參數:

路徑字符串|函數您要重定向到的url路徑或返回url路徑的函數規則。函數版本傳遞兩個參數:$ injector和$ location。

app.config(function($urlRouterProvider){ 
    // if the path doesn't match any of the urls you configured 
    // otherwise will take care of routing the user to the specified url 
    $urlRouterProvider.otherwise('/index'); 

    // Example of using function rule as param 
    $urlRouterProvider.otherwise(function($injector, $location){ 
     ... some advanced code... 
    }); 
}) 

如何使用.when()正確(例如爲了申報請也在這裏(更多細節和例子)

+0

謝謝。在我的情況下,我已經在使用最新的路由器,所以我想我需要選擇onChangeConfig選項。你可以添加一個例子,說明如何使用它來滿足我的特殊需求,我想要使用/ Auth/confirmation來表示狀態。謝謝 – Alan2

+1

會做,給我秒 –

+0

也只是爲了確認。 app.config(stateConfig)的順序是什麼? app.config(onChangeConfig);哪一個應該先來。順便說一下,您是否有任何想法,以確定在下一個版本或最新的提交中,如果這些不起作用將會被修復。謝謝 – Alan2

1

這是很簡單的實際,但需要在服務器端的工作:必須將服務器配置成服務index.html頁面的路徑/Auth/confirmation(併爲您的應用程序的所有其他可收藏的網址)。

一旦這是真的,一個用戶去/Auth/confirmation將因此下載index.html文件,這將啓動角應用程序。 ui路由器模塊將分析位置,加載相應的狀態並顯示相應的視圖。

+0

謝謝,但問題是,我會發出一封電子郵件,要求人們點擊這樣的事情:http:// localhost:2757/Auth/confirmation我想我知道一種方法來配置服務器,使其發揮作用到index.html,但我怎麼能使它自動進入ui-router/Auth/Confirmation狀態? – Alan2

+0

你沒有任何事情要做。路由器自動執行該操作:只要應用程序啓動,它就會分析該位置並進入相應的狀態。 –

+0

我更新了一些我發現的問題。我在考慮在index.html之後添加內容可能會起作用,但看起來不行。 – Alan2

相關問題