每次调用子视图时实例化父控制器
Parent controller instantiated each time a child view is called
我正在使用 ui-router 并且我有一个配置,其中我使用的父视图有一个控制器,子视图也有自己的控制器。
每次我导航到其中一个子视图时,父控制器都会再次实例化,因此会对服务器执行不必要的请求。
我希望能够在子状态之间导航并重用父控制器解析的变量。
如何防止父控制器每次都被实例化?
这是我的路由配置:
$stateProvider
.state('researchLayoutEditor', {
url: '/research/:researchId/layoutEditor/:orderInGroup',
controller: 'layoutEditorController',
controllerAs: 'layoutEditorCtrl',
templateUrl: 'app/researchManagement/researchLayoutEditor/layoutEditor.html',
redirectTo: 'researchLayoutEditor.slide',
params: {
group: 'A',
orderInGroup: '0'
},
resolve: {
researchLayout: function (researchesService, $stateParams) {
return researchesService.getResearchLayout($stateParams.researchId, false);
},
research: function (researchesService, $stateParams) {
return researchesService.getResearch($stateParams.researchId);
}
}
}).state('researchLayoutEditor.slide', {
url: '/textual',
templateUrl: 'app/researchManagement/researchLayoutEditor/textSlide.html',
controller: 'textSlideController',
controllerAs: 'txtSlide'
}).state('researchLayoutEditor.movie', {
url: '/video',
templateUrl: 'app/researchManagement/researchLayoutEditor/movieSlide.html',
controller: 'movieSlideController',
controllerAs: 'movieSlide',
resolve: {
movieCategories: function (utilsService) {
return utilsService.getVideoCategories();
}
}
}).state('researchLayoutEditor.memory', {
url: '/memory',
templateUrl: 'app/researchManagement/researchLayoutEditor/memorySlide.html',
controller: 'memorySlideController',
controllerAs: 'memorySlide'
}).state('researchLayoutEditor.question', {
url: '/question',
templateUrl: 'app/researchManagement/researchLayoutEditor/questionSlide.html',
controller: 'questionSlideController',
controllerAs: 'questionSlide'
});
这是 HTML:
<div id="mainContent">
<!-- our nested state views will be injected here -->
<div ui-view class="inner-content"></div>
</div>
尝试使用父状态作为抽象,
这是一个 plnkr
示例 check it out
好吧,在尝试了最有用的消除调试方法之后,我发现了我构建 URL 的方式的问题。
在我的 parent 状态下,我的 URL 定义如下:
url: '/research/:researchId/layoutEditor/:orderInGroup',
当 orderInGroup 参数在每次单击 child 状态时发生变化。所以最后的 URL 看起来像这样,例如:
.../#/research/37/layoutEditor/1/video
Apparently,改变"orderInGroup"参数的位置后放到URL
的末尾
.../#/research/37/layoutEditor/video/1
并移动
params: {
group: 'A',
orderInGroup: '0'
}
object 到 child 状态,问题已解决。
所以最终工作的 URL 配置看起来像这样(我将 /:orderInGroup 移动到 child 状态):
$stateProvider
.state('researchLayoutEditor', {
url: '/research/:researchId/layoutEditor',
controller: 'layoutEditorController',
controllerAs: 'layoutEditorCtrl',
templateUrl: 'app/researchManagement/researchLayoutEditor/layoutEditor.html',
redirectTo: 'researchLayoutEditor.slide'
})
.state('researchLayoutEditor.movie', {
url: '/video/:orderInGroup',
templateUrl: 'app/researchManagement/researchLayoutEditor/movieSlide.html',
controller: 'movieSlideController',
controllerAs: 'movieSlide',
params: {
group: 'A',
orderInGroup: '0'
}
})
.state('researchLayoutEditor.slide', {
url: '/textual/:orderInGroup',
templateUrl: 'app/researchManagement/researchLayoutEditor/textSlide.html',
controller: 'textSlideController',
controllerAs: 'txtSlide',
params: {
group: 'A',
orderInGroup: '0'
}
});
我正在使用 ui-router 并且我有一个配置,其中我使用的父视图有一个控制器,子视图也有自己的控制器。 每次我导航到其中一个子视图时,父控制器都会再次实例化,因此会对服务器执行不必要的请求。 我希望能够在子状态之间导航并重用父控制器解析的变量。
如何防止父控制器每次都被实例化?
这是我的路由配置:
$stateProvider
.state('researchLayoutEditor', {
url: '/research/:researchId/layoutEditor/:orderInGroup',
controller: 'layoutEditorController',
controllerAs: 'layoutEditorCtrl',
templateUrl: 'app/researchManagement/researchLayoutEditor/layoutEditor.html',
redirectTo: 'researchLayoutEditor.slide',
params: {
group: 'A',
orderInGroup: '0'
},
resolve: {
researchLayout: function (researchesService, $stateParams) {
return researchesService.getResearchLayout($stateParams.researchId, false);
},
research: function (researchesService, $stateParams) {
return researchesService.getResearch($stateParams.researchId);
}
}
}).state('researchLayoutEditor.slide', {
url: '/textual',
templateUrl: 'app/researchManagement/researchLayoutEditor/textSlide.html',
controller: 'textSlideController',
controllerAs: 'txtSlide'
}).state('researchLayoutEditor.movie', {
url: '/video',
templateUrl: 'app/researchManagement/researchLayoutEditor/movieSlide.html',
controller: 'movieSlideController',
controllerAs: 'movieSlide',
resolve: {
movieCategories: function (utilsService) {
return utilsService.getVideoCategories();
}
}
}).state('researchLayoutEditor.memory', {
url: '/memory',
templateUrl: 'app/researchManagement/researchLayoutEditor/memorySlide.html',
controller: 'memorySlideController',
controllerAs: 'memorySlide'
}).state('researchLayoutEditor.question', {
url: '/question',
templateUrl: 'app/researchManagement/researchLayoutEditor/questionSlide.html',
controller: 'questionSlideController',
controllerAs: 'questionSlide'
});
这是 HTML:
<div id="mainContent">
<!-- our nested state views will be injected here -->
<div ui-view class="inner-content"></div>
</div>
尝试使用父状态作为抽象,
这是一个 plnkr
示例 check it out
好吧,在尝试了最有用的消除调试方法之后,我发现了我构建 URL 的方式的问题。 在我的 parent 状态下,我的 URL 定义如下:
url: '/research/:researchId/layoutEditor/:orderInGroup',
当 orderInGroup 参数在每次单击 child 状态时发生变化。所以最后的 URL 看起来像这样,例如:
.../#/research/37/layoutEditor/1/video
Apparently,改变"orderInGroup"参数的位置后放到URL
的末尾.../#/research/37/layoutEditor/video/1
并移动
params: {
group: 'A',
orderInGroup: '0'
}
object 到 child 状态,问题已解决。 所以最终工作的 URL 配置看起来像这样(我将 /:orderInGroup 移动到 child 状态):
$stateProvider
.state('researchLayoutEditor', {
url: '/research/:researchId/layoutEditor',
controller: 'layoutEditorController',
controllerAs: 'layoutEditorCtrl',
templateUrl: 'app/researchManagement/researchLayoutEditor/layoutEditor.html',
redirectTo: 'researchLayoutEditor.slide'
})
.state('researchLayoutEditor.movie', {
url: '/video/:orderInGroup',
templateUrl: 'app/researchManagement/researchLayoutEditor/movieSlide.html',
controller: 'movieSlideController',
controllerAs: 'movieSlide',
params: {
group: 'A',
orderInGroup: '0'
}
})
.state('researchLayoutEditor.slide', {
url: '/textual/:orderInGroup',
templateUrl: 'app/researchManagement/researchLayoutEditor/textSlide.html',
controller: 'textSlideController',
controllerAs: 'txtSlide',
params: {
group: 'A',
orderInGroup: '0'
}
});