I have a modal window that I use to present a form to users. They enter the information and then press a button the has an ng-click. The server processes the request and sends back a response. When the response is success I want to close the modal window from the controller. How can this be achieved?
The modal is a partial included in another page
Main page:
<!-- main content --> <p>Foo</p> <!-- angular directive --> <foo-directive></foo-directive>
Content of that directive:
<div ng-controller="FooCtrl"> <ul class="thumbnails"> <li class="span3 tile tile-white" ng-repeat="foo in model.foo"> <div> {{foo.bar}} </div> <div> ({{foo.bam}}) </div> <div> <a data-toggle="modal" href="#myModal"><img src="{{foo.imgPath}}"></a> </div> </li> </ul> <!-- foo modal partial included by ejs --> <% include foo_modal.ejs %> </div>
Modal markup:
<div id="fooModal" class="modal hide fade in" style="display: none; "> <div class="modal-header"> <a class="close" data-dismiss="modal">×</a> <h3>New Device</h3> </div> <div class="modal-body"> <h4>Foo Modal</h4> <div ng-controller="FooCtrl"> <form name="fooFrm"> <input id="email" type="email" class="input-medium" ng-model="fooEmail" placeholder="Email"> <button class="btn btn-primary btn-small" ng-click="doFoo({email:fooEmail})">Email Link</button> </form> </div> </div> <div class="modal-footer"> <a href="#" class="btn" data-dismiss="modal">Close</a> </div> </div>
Controller code:
functionFooCtrl($scope, FooService) { $scope.doFoo= function (email) { FooService.save({email:email.fooEmail}) { alert('Request successful'); //TODO close Twitter bootstrap modal named fooModal here }, function (err) { alert('Your request bonked, sorry'); //TODO close twitter bootstrap modal named fooModal here }); } };
What is the right way to close the modal from the controller in the success and error functions?
Thanks in advance,
Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.
When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.
use click call to actually close the modal and remove the overlay.
We can achieve the same without using angular-ui. This can be done using angular directives.
First add the directive to the modal.
<div class="modal fade" my-modal ....>...</div>
Create a new angular directive:
app.directive('myModal', function() { return { restrict: 'A', link: function(scope, element, attr) { scope.dismiss = function() { element.modal('hide'); }; } } });
Now call the dismiss() method from your controller.
app.controller('MyCtrl', function($scope, $http) { // You can call dismiss() here $scope.dismiss(); });
I am still in my early days with angular js. I know that we should not manipulate the DOM inside the controllers. So I have the DOM manipulation in the directive. I am not sure if this is equally bad. If I have a better alternative, I shall post it here.
The important thing to note is that we cannot simply use ng-hide or ng-show in the view to hide or show the modal. That simply hides the modal and not the modal backdrop. We have to call the modal() instance method to completely remove the modal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With