Closing Twitter Bootstrap Modal From Angular Controller

后端 未结 8 826
时光说笑
时光说笑 2020-12-12 21:26

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 s

8条回答
  •  生来不讨喜
    2020-12-12 21:52

    We can achieve the same without using angular-ui. This can be done using angular directives.

    First add the directive to the modal.

    
    

    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.

提交回复
热议问题