43

我有一个模态窗口,用于向用户呈现表单。他们输入信息,然后按下具有 ng-click 的按钮。服务器处理请求并发送回响应。当响应成功时,我想从控制器关闭模式窗口。如何做到这一点?

模态是另一个页面中包含的部分

主页:

<!-- main content -->
<p>Foo</p>
<!-- angular directive -->
<foo-directive></foo-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>

模态标记:

<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>

控制器代码:

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

在成功和错误功能中从控制器关闭模式的正确方法是什么?

提前致谢,

4

8 回答 8

53

我们可以在不使用 angular-ui 的情况下实现相同的效果。这可以使用角度指令来完成。

首先将指令添加到模态。

<div class="modal fade" my-modal ....>...</div>

创建一个新的角度指令:

app.directive('myModal', function() {
   return {
     restrict: 'A',
     link: function(scope, element, attr) {
       scope.dismiss = function() {
           element.modal('hide');
       };
     }
   } 
});

现在从你的控制器调用dismiss() 方法。

app.controller('MyCtrl', function($scope, $http) {
    // You can call dismiss() here
    $scope.dismiss();
});

我还处于使用 Angular js 的早期阶段。我知道我们不应该在控制器内部操作 DOM。所以我在指令中有 DOM 操作。我不确定这是否同样糟糕。如果我有更好的选择,我会在这里发布。

需要注意的重要一点是,我们不能简单地在视图中使用 ng-hide 或 ng-show 来隐藏或显示模态框。这只是隐藏了模态而不是模态背景。我们必须调用 modal() 实例方法来完全删除模态。

于 2013-09-12T12:53:00.537 回答
35

你可以这样做:

angular.element('#modal').modal('hide');
于 2016-03-01T17:18:20.013 回答
22

你看过angular-ui bootstrap吗?有一个 Dialog (ui.bootstrap.dialog) 指令效果很好。您可以在回调期间以角度方式关闭对话框(根据示例):

$scope.close = function(result){
  dialog.close(result);
};

更新:

该指令已重命名为Modal

于 2013-05-17T09:10:14.733 回答
5

这是一个可重用的 Angular 指令,它将隐藏和显示 Bootstrap 模式。

app.directive("modalShow", function () {
    return {
        restrict: "A",
        scope: {
            modalVisible: "="
        },
        link: function (scope, element, attrs) {

            //Hide or show the modal
            scope.showModal = function (visible) {
                if (visible)
                {
                    element.modal("show");
                }
                else
                {
                    element.modal("hide");
                }
            }

            //Check to see if the modal-visible attribute exists
            if (!attrs.modalVisible)
            {

                //The attribute isn't defined, show the modal by default
                scope.showModal(true);

            }
            else
            {

                //Watch for changes to the modal-visible attribute
                scope.$watch("modalVisible", function (newValue, oldValue) {
                    scope.showModal(newValue);
                });

                //Update the visible value when the dialog is closed through UI actions (Ok, cancel, etc.)
                element.bind("hide.bs.modal", function () {
                    scope.modalVisible = false;
                    if (!scope.$$phase && !scope.$root.$$phase)
                        scope.$apply();
                });

            }

        }
    };

});

使用示例 #1 - 假设您要显示模式 - 您可以添加 ng-if 作为条件

<div modal-show class="modal fade"> ...bootstrap modal... </div>

使用示例 #2 - 在 modal-visible 属性中使用 Angular 表达式

<div modal-show modal-visible="showDialog" class="modal fade"> ...bootstrap modal... </div>

另一个示例 - 为了演示控制器交互,您可以在控制器中添加类似这样的内容,它将在 2 秒后显示模式,然后在 5 秒后将其隐藏。

$scope.showDialog = false;
$timeout(function () { $scope.showDialog = true; }, 2000)
$timeout(function () { $scope.showDialog = false; }, 5000)

我迟到了这个问题 - 在这里为另一个问题创建了这个指令。Bootstrap 模态的简单 Angular 指令

希望这可以帮助。

于 2013-10-29T20:39:02.417 回答
1

您可以将 data-dismiss="modal" 添加到调用 angularjs 功能的按钮属性中。

如;

<button type="button" class="btn btn-default" data-dismiss="modal">Send Form</button>
于 2015-04-21T07:33:57.830 回答
0

我已将@isubuz 的答案@Umur Kontacı 关于属性指令的答案重新混合到一个版本中,在该版本中,您的控制器不会调用“dismiss”之类的类似 DOM 的操作,而是尝试使用更多 MVVM 样式,设置一个布尔值财产isInEditMode。视图反过来将这些信息链接到打开/关闭引导模式的属性指令。

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;
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.css" rel="stylesheet"/>

<div ng-app="myApp" ng-controller="MyCtrl as vm">

<div class="modal fade" my-modal my-modal-is-open="isInEditMode">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-body">
        Modal body! IsInEditMode == {{isInEditMode}}
      </div>
      <div class="modal-footer">
        <button class="btn" ng-click="toggleEditMode()">Close</button>
      </div>
    </div>
  </div>
</div>

<p><button class="btn" ng-click="toggleEditMode()">Toggle Edit Mode</button></p> 
<pre>isInEditMode == {{isInEditMode}}</pre>
  
</div>

于 2016-07-12T05:53:55.810 回答
-1
**just fire bootstrap modal close button click event**
var app = angular.module('myApp', []);
app.controller('myCtrl',function($scope,$http){
  $('#btnClose').click();// this is bootstrap modal close button id that fire click event
})

--------------------------------------------------------------------------------

<div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
        </div>
        <div class="modal-body">
          <p>Some text in the modal.</p>
        </div>
        <div class="modal-footer">
          <button type="button" id="btnClose" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>

只需在设置 btnClose 时设置模态“关闭按钮”ID,要以角度关闭模态,您必须像我一样触发关闭按钮单击事件 $('#btnClose').click()

于 2017-07-14T17:33:39.877 回答
-2

您可以使用简单的 jquery 代码来完成。

$('#Mymodal').modal('hide');
于 2015-06-05T15:33:34.543 回答