簡體   English   中英

角度ui路由器-嵌套控制器-如果子控制器處於相同狀態,則父控制器啟動兩次

[英]angular ui-router - nested controller - parent controller is initiated twice if child controller is in the same state

.state('home', {
    url: '/home',
    views: {
        'main': {
            templateUrl: './main.html',
            controller: 'mainController'
        },
        'parent@home': {
            templateUrl: './parent.html',
            controller: 'parentController'
        },
        'child@home':{
            templateUrl: './child.html',
            controller: 'childController'
        },
        'otherPage@home': {
            templateUrl: './other-page.html',
            controller: 'otherController'
        }
    }
})

在parent.html中,我有子視圖。 基本上,父控制器(parentController)在此設置中啟動了兩次。 如果我刪除child @ home狀態,則parentController僅啟動一次。 我在做什么錯,有沒有更好的方法來定義這些狀態?

index.html

<div class="maincontent" ui-view="main"></div>

main.html

<div class="maincontent container-fluid" ui-view="parent"></div>
<div class="maincontent container-fluid" ui-view="otherPage"></div>

parent.html

<div ui-view="child"></div>

像這樣重新定義狀態:

.state('home', {
    url: '/home',
    views: {
        '': {
            templateUrl: './main.html',
            controller: 'mainController'
        },
        'parent@home': {
            templateUrl: './parent.html',
            controller: 'parentController'
        },
        'child@home':{
            templateUrl: './child.html',
            controller: 'childController'
        },
        'otherPage@home': {
            templateUrl: './other-page.html',
            controller: 'otherController'
        }
    }
})

根據我的理解,帶有空鍵的視圖將被加載為原始狀態的默認視圖。

這樣,您的main.html模板將包含指向其他ui-views指針,就像您對parentotherPage所做的otherPage

我無法進行測試,因為我不在開發PC上,但我認為它應該可以工作,因為它是我的幾個AngularJS項目遵循的格式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM