2015-10-17 40 views
1

我嘗試建立與AngularJS的網站,我使用Ui路由器擴展爲此。我想問的是如何配置「主頁」按鈕?ui路由器回主頁按鈕

HTML:

<body> 
    <a href="#">Home Page</a> 
<div ui-view> 
<div> 

    <a ui-sref="A">State A</a> 
    <a ui-sref="B">State B</a> 

</div> 
</div> 

的Javascript:無狀態的index.html。它是不是必需的?

.$state('A', { 
     url:"/A", 
     templateUrl:"A.html", 
     controller:"aController" 
}) 
.$state('B', { 
     url:"/B", 
     templateUrl:"B.html", 
     controller:"bController" 
})  
+0

我想你可以使用配置主頁路由器提供商。 –

回答

2

您需要根據您的HTML一旦UI路由器創建一個默認路由

// For any unmatched url, redirect to/(aka default) 
$urlRouterProvider.otherwise("/"); 

// other routes 
.... 
.$state('default', { 
    url:"/", 
    templateUrl:"default.html", 
    controller: "" 
}) 

替換UI視圖內的內容,這2個鏈接將會消失,您需要一個包含這些鏈接的模板。

的index.html

<body> 
    <a href="#">Home Page</a> 
<div ui-view> <!-- The contents of this div are replaced with the states template --> 
</div> 
</body> 

default.html中

<div> 
    <a ui-sref="A">State A</a> 
    <a ui-sref="B">State B</a> 
</div> 
+0

好的,這是工程,但我認爲控制器有問題。範圍值不會返回到indexCtrl值。 – Nasuh

0

我會創造一個家的狀態,並使用類似的方法爲A和B