簡體   English   中英

多次實例化Angular UI Router控制器-直接路由

[英]Angular UI Router controllers instantiated multiple times - Direct route

我已經設置了父/子路由,當我使用state.go()在代碼中更改狀態時,一切工作都很好。 當我嘗試僅使用URL直接進入子路線時,就會出現問題。 當我這樣做時,控制器將被初始化多次。

這是我的app.js

    $stateProvider
    .state('home', {
        url: "/",
        templateUrl: "Views/home.html",
        controller: 'HomeController',
        controllerAs: 'vm'
    })
    .state('ticketsMaster', {
        abstract: true,
        templateUrl: "Views/tickets-master.html",
        url: "^/performances?featureCode&theatreId",
        controller: 'TicketsMasterController',
        controllerAs: 'vm'
    })
    .state('ticketsMaster.performances', {
        url: "^/performances?featureCode&theatreId",
        templateUrl: "Views/performances.html",
        controller: 'PerformancesController',
        controllerAs: 'vm'
    })
    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });

home.html是我的用戶界面視圖的主頁

我的tickets-master.html上還有另一個ui視圖,該視圖加載了我的孩子視圖(表演)。

當直接轉到/performances?featureCode=1111&theatreId=1它將加載我的TicketsMasterController和Performances控制器2或3次。

任何幫助,將不勝感激。

我在這里找到了答案:

https://github.com/angular-ui/ui-router/issues/372

將此行添加到app.config中:

$locationProvider.html5Mode(true).hashPrefix('!')

並將此行包含在index.html的開頭:

<base href="/">

暫無
暫無

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

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