[英]Angular UI Router controllers instantiated multiple times - Direct route
I have a parent / child route set up, and everything works great when I change the state in code using state.go(). 我已经设置了父/子路由,当我使用state.go()在代码中更改状态时,一切工作都很好。 The problem arises when I try to go directly to the child route using only a URL.
当我尝试仅使用URL直接进入子路线时,就会出现问题。 When I do this, the controllers get initialized multiple times.
当我这样做时,控制器将被初始化多次。
Here is my app.js 这是我的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 is my master page where I have my ui-view home.html是我的用户界面视图的主页
I also have another ui-view on my tickets-master.html where this loads my child views (performances). 我的tickets-master.html上还有另一个ui视图,该视图加载了我的孩子视图(表演)。
When going directly to /performances?featureCode=1111&theatreId=1
it loads my TicketsMasterController
, and Performances controller 2 or 3 times. 当直接转到
/performances?featureCode=1111&theatreId=1
它将加载我的TicketsMasterController
和Performances控制器2或3次。
Any help would be appreciated. 任何帮助,将不胜感激。
I found the answer here: 我在这里找到了答案:
https://github.com/angular-ui/ui-router/issues/372 https://github.com/angular-ui/ui-router/issues/372
add this line to app.config:
将此行添加到app.config中:
$locationProvider.html5Mode(true).hashPrefix('!')
and include this line in the head of index.html:
并将此行包含在index.html的开头:
<base href="/">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.