棱角分明的UI引导模式,而无需创建新的控制器 [英] Angular-ui bootstrap modal without creating new controller

查看:125
本文介绍了棱角分明的UI引导模式,而无需创建新的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

普拉克: http://plnkr.co/edit/85Wl5W 如果我使用$ modalInstance同一控制器上(modalController .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.

我只是想用angularjs与角UI引导模式的服务,以简化我的生活。我想用同一个控制器在一个单独的文件中,但在一个模式为好。也就是说,我希望他们做同样的任务。有没有做任何正确的方法?

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.

由于提前,

亚历

推荐答案

我还没有找到一个没有干净的解决方案,我找到了最好的解决方法,避免写同一code两次是扩展模态控制器一样这样的:

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');
    };

}]);

这样的话,我可以重新使用相同的code,而不必一切从头改写它,我可以覆盖我想做的事情不便不同于原始控制器的功能。

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.

希望我帮助一些人在那里,

Hope I helped some people out there,

亚历

这篇关于棱角分明的UI引导模式,而无需创建新的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆