Closing Twitter Bootstrap Modal From Angular Controller

后端 未结 8 836
时光说笑
时光说笑 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:56

    I have remixed the answer by @isubuz and this answer by @Umur Kontacı on attribute directives into a version where your controller doesn't call a DOM-like operation like "dismiss", but instead tries to be more MVVM style, setting a boolean property isInEditMode. The view in turn links this bit of info to the attribute directive that opens/closes the bootstrap modal.

    var app = angular.module('myApp', []);
    
    app.directive('myModal', function() {
       return {
         restrict: 'A',
         scope: { myModalIsOpen: '=' },
         link: function(scope, element, attr) {
           scope.$watch(
             function() { return scope.myModalIsOpen; },
             function() { element.modal(scope.myModalIsOpen ? 'show' : 'hide'); }
           );
         }
       } 
    });
    
    app.controller('MyCtrl', function($scope) {
      $scope.isInEditMode = false;
      $scope.toggleEditMode = function() { 
        $scope.isInEditMode = !$scope.isInEditMode;
      };
    });
    
    
    
    
    
    

    isInEditMode == {{isInEditMode}}

提交回复
热议问题