2016-09-24 57 views
0

在我的默認主頁中,我有1個導航欄和2個項目。angular.js:13550錯誤:無法從狀態'mainpath'中解析'item1'

我的主界面視圖配置爲:

angular.module('app').config([... 
    $urlRouterProvider.otherwise('/mainpath'); 
    $stateProvider 
     .state('mainpath', { 
     url: '/mainpath', 
     template: '<main-html></main-html>' 
     }); 
... ]); 

所以,當我去我的網站,默認情況下它會指向到localhost:8080 /主路徑。但是,因爲我的主頁中有一個導航條,其中有兩個項目,我將所選項目設置爲item1,而item1的md-nav-sref指向「item1State」。所以我期待當我去localhost:8080,它不會去localhost:8080/mainpath,但會直接到localhost:8080/mainpath/item1 ..我的意思是這裏的URL會改變,但內容因爲我的main-html文件包含導航條。我只想將item1 html加載到main-html的ui-view中,該html目前沒有發生。

我對於被默認加載的視圖配置爲:

angular.module('app').config([... 
    $stateProvider 
     .state('item1State', { 
     url: '/item1', 
     template: '<item1></item1>' 
     }) 
     .state('item2State', { 
     url: '/item2', 
     template: '<item2></item2>' 
     }); 
... ]); 

當我試圖切換到不同的導航欄項目,我得到的錯誤:「無法從國家‘主路徑’解決‘物品1’ 「,與item2相同。 所以我有2個問題,一個是我期待我的網址將localhost:8080/mainpath/item1,因爲我有默認情況下,當我加載我的主頁時選擇了導航欄item1。接下來是選擇導航欄項目時出現此錯誤。 我是angularjs的新手,我希望你能理解。謝謝

+1

你的路由器都是獨立的,你是不是嵌套它們。要在子中使用父路由,您需要以這種方式進行配置。按照這個https://github.com/angular-ui/ui-router/wiki/nested-states-&-nested-views#abstract-state-usage-examples –

+1

哇!非常感謝。我仍然需要了解更多有關angularjs的信息。我會繼續閱讀,並感謝你的快速回答,現在一切正常。 – iPhoneJavaDev

+0

我應該把答案放在答案中,如果你能接受它? –

回答

1

兩個你的路由器是分開的,你不是嵌套它們。要在子中使用父路由,您需要以這種方式進行配置。

$stateProvider 
.state('contacts', { 
    abstract: true, 
    url: '/contacts', 

    // Note: abstract still needs a ui-view for its children to populate. 
    // You can simply add it inline here. 
    template: '<ui-view/>' 
}) 
.state('contacts.list', { 
    // url will become '/contacts/list' 
    url: '/list' 
    //...more 
}) 
.state('contacts.detail', { 
    // url will become '/contacts/detail' 
    url: '/detail', 
    //...more 
}) 

更多信息here

相關問題