与控制器AngularStrap密切模式 [英] AngularStrap close modal with controller
问题描述
我使用AngularStrap与引导。
I'm using AngularStrap with bootstrap.
我有一个使用它自己的控制器中的模态对话框。我怎样才能用这个本地控制器关闭模式?
I have a modal dialog that uses it's own controller. How can I close the modal using this local controller?
我实例化一个像这样的按钮控制器:
I instantiate the controller on a button like this:
<button type="button"
class="btn btn-success btn-lg"
bs-modal="modal"
data-template="user-login-modal.html"
data-container="body"
ng-controller="userLoginController"
>Click here to log in</button>
和userLoginController有这样的:
and the userLoginController has this:
$scope.authenticate = function(){
this.hide(); // this doesn't work
}
这显然只是一个演示,我希望它关闭在成功登录,但是这哪里是code我会用它来关闭它会去。
This is obviously just a demo, I want it to close on successful login, but this is where the code I'd use to close it would go.
我试过编程实例化模式(使用$模式服务创建模式),但我一直无法弄清楚如何通过这种方法注入控制器。
I've tried instantiating the modal programmatically (use the $modal service to create the modal) but I haven't been able to figure out how to inject the controller through that method.
如果我是做这样的事情,从发射使用BS模态指令模态,我怎么可以参考模态的事件来关闭它?
If I were to do something like emit an event from the modal using the bs-modal directive, how can I reference the modal to close it?
这是我的plnkr:
http://plnkr.co/edit/m5gT1HiOl1X9poicWIEi?p=$p$pview一>
here's my plnkr: http://plnkr.co/edit/m5gT1HiOl1X9poicWIEi?p=preview
推荐答案
想出了一个好办法:
我感动NG-控制器模板,并使用所提供的服务模式实例化模式。然后我用一个rootscope撒施让大家知道有人成功登录。
I moved the ng-controller to the TEMPLATE and instantiate the modal using the provided modal service. I then use a rootscope broad cast to let everyone know that someone successfully logged in.
新的控制器code:
var loginModal = $modal({template:'/template.html', show:false});
$scope.showLogin = function(){
loginModal.$promise.then(loginModal.show);
}
$scope.$on("login", function(){
loginModal.$promise.then(loginModal.hide);
});
按钮只是看起来像现在这样:
the button just looks like this now:
<button type="button"
class="btn btn-success btn-lg"
ng-click="showLogin()"
>Click here to log in</button>
和我的模板已在第一个标签老NG-控制器。
and my template has the old ng-controller in the first tag.
这篇关于与控制器AngularStrap密切模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!