是否可以使用单例控制器获得多个视图[ https://github.com/angular-ui/ui-router/issues/494]?
使用案例:我有一个ui-view = header和ui-view = content.我根据当前状态更改标题以显示上下文相关按钮(即返回,保存,过滤等)我希望这些按钮在内容控制器中调用函数,但如果我执行以下操作,则会创建两个MyController对象.如果有任何init函数,它们会被调用两次,这在大多数情况下是对我的服务器的查询的两倍.
views:{
'header':{
templateURL:'...',
controller:'MyController'
},
'content':{
templateURL:'...',
controller:'MyController'
}
}
Run Code Online (Sandbox Code Playgroud)
更新:基于@pankajparkar
我当前的index.html看起来像这样(为了理解而简化)
<nav ui-view="header"></nav>
<div ui-view="content"></div>
Run Code Online (Sandbox Code Playgroud)
但你的建议包括/ REQUIRE子视图
<!--index.html-->
<body ui-view></body>
<!--format.html-->
<nav ui-view="header"></nav>
<div ui-view="content"></div>
Run Code Online (Sandbox Code Playgroud)
使用以下控制器
state('app', {
url: '/app',
controller: 'MyController',
templateURL: 'format.html', //Targets ui-view in index.html
views:{
'header':{
templateURL:'...', //Targets ui-view="header" in format.html
},
'content':{
templateURL:'...', //Targets ui-view="header" in content.html
}
}
});
Run Code Online (Sandbox Code Playgroud)