2015-10-05 51 views
1

我有一個$ routeProvider的問題。

我有2個觀點。第一個視圖是用戶列表。第二個,是用戶的個人資料。

在我的第一個觀點,我指定的鏈接:

<a ng-href="#/view2/{{user.login}}">{{user.login}}</a> 

在視圖2的js文件,我指定:

.config(['$routeProvider', function($routeProvider) { 
    $routeProvider.when('/view2/:user', { 
    templateUrl: 'view2/view2.html', 
    controller: 'View2Ctrl' 
    }); 
}]) 

而現在,每當我試着點擊鏈接,它只是將url更改爲以下內容:

path-to-the-file:/index.html#/view2/blabla其中blabla是user.login

那麼我該如何讓它工作?

謝謝!

+1

你需要從'http'服務器加載頁面而不是'file://'。輕鬆在電腦上安裝網頁服務器 – charlietfl

+0

哦。有關如何做到這一點的任何建議? – uksz

+0

如果您在Windows PC上,只需在IIS中進行設置即可。 https://technet.microsoft.com/en-gb/library/cc725762.aspx – cullimorer

回答

1

使用ngRoute和ui路由器後,我肯定會使用ui路由器進行路由(https://github.com/angular-ui/ui-router)。

ui-router爲日常任務(如鏈接)提供了更廣泛的API。例如,你可以使用自定義指令稱爲UI-SREF:

<a ui-sref="contacts.detail({ id: contact.id })">{{ contact.name }}</a> 

所以你不必來組裝自己的鏈接,框架會爲你。

+0

設置ui-router與routeProvider相同嗎? – uksz

+0

我最近從ngRoute升級到了ui-router,它非常快。在這裏閱讀:https://github.com/angular-ui/ui-router/wiki – imriqwe