问题描述
plunk: http://plnkr.co/edit/85Wl5W 如果我在同一个控制器(modalController)上使用 $modalInstance.js),没有处于模态,角度会被冻结.
plunk: http://plnkr.co/edit/85Wl5W If I use the $modalInstance on the same controller(modalController.js), without being in a modal, angular gets frozen.
我只是想通过 angular-ui 引导模式服务使用 angularjs 来简化我的生活.我想在单独的文件上使用相同的控制器,但也想在模态上使用.也就是说,我希望他们做同样的任务.有什么正确的方法可以做到吗?
I just want to simplify my life using angularjs with the angular-ui bootstrap modal service. I want to use the same controller on a separate file, but on a modal as well. That is, I want them to do the same task. Is there any proper way to do it?
例如modal.html、modalController.js.我想在模态窗口上显示这些,但在我的应用程序中也没有模态窗口.问题是,如果我使用相同的控制器,则无法注入 $modalInstance,因为如果没有模态,则它是未定义的.
E.g. modal.html, modalController.js. I want to show these on a modal window, but on my application as well without a modal. The issue is that if I use the same controller I can't inject the $modalInstance, as it's undefined if there's no modal.
提前致谢,
亚历克斯
推荐答案
我还没有找到一个干净的解决方案,我找到的最好的解决方法是避免编写相同的代码两次是像这样扩展模态控制器:
I haven't found a clean solution yet, the best workaround I found, to avoid writing the same code twice was to extend the modal controller like this:
$.extend(this, $controller('NormalCtrl', {$scope: $scope}));
完整控制器:
.controller('ModalCtrl', ['$scope', '$controller', '$modalInstance',
function ($scope, $controller, $modalInstance) {
//with this line here:
// Initialize the super class and extend it.
$.extend(this, $controller('NormalCtrl', {$scope: $scope}));
// Opens a search result
$scope.openResult = function() {
$modalInstance.close($scope.selectedRow);
};
// Called when the cancel button is pressed
$scope.back = function() {
$modalInstance.dismiss('cancel');
};
}]);
那样,我可以重用相同的代码,而不必重新重写它,并且我可以覆盖我想要做的与原始控制器不同的功能.
That way, I can re-use the same code, without having to rewrite it all over again and I can override the functions that I want to do smth different to the original controller.
希望我帮助了一些人,
亚历克斯
这篇关于Angular-ui 引导模式,无需创建新控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!