【问题标题】:Closing Twitter Bootstrap Modal From Angular Controller从 Angular 控制器关闭 Twitter 引导模式
【发布时间】:2013-03-02 06:03:14
【问题描述】:

我有一个模式窗口,用于向用户展示表单。他们输入信息,然后按下具有 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
            });
        }
    };

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

提前致谢,

【问题讨论】:

  • 能否提供您的FooService 的代码?非常感谢提前...
  • 请考虑切换您接受的答案,以便我删除我的。我厌倦了收到投票通知。 :-)

标签: twitter-bootstrap angularjs angularjs-directive


【解决方案1】:

我们可以在不使用 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() 实例方法来完全删除模态。

【讨论】:

  • 请注意,为了使其工作,jquery.jsbootstrap-modal.js 必须在 angular.js 之前加载,否则元素对象将没有 modal() 函数。
  • 嗨!我想对同一页面上的 2 个模式执行此操作。我该怎么做?
  • 嘿,这不起作用(至少在我的情况下)。如果我们在link 中添加scope.dismiss(),我认为我们不能在separate controller 中使用它。我尝试了您的解决方案,但它不起作用。从view 调用它 - 例如ng-click="dismiss();" 有效,但是,即使controller 覆盖directive 不起作用,也将它带到不同的controller
  • @Syl,但是在控制器内部访问 dom 元素 $(element).modal 是一个好习惯吗?这不应该在服务中完成
  • 嗨 isubuz,你能回答这个问题吗stackoverflow.com/questions/43583136/…
【解决方案2】:

你可以这样做:

angular.element('#modal').modal('hide');

【讨论】:

    【解决方案3】:

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

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

    更新:

    该指令已重命名为Modal

    【讨论】:

    • 关闭对话框会导致对话框中的表单提交。有什么办法可以停止并关闭?
    • 我觉得angular-ui bootstrap在bootstrap官方发布的功能和样式上并不完整。
    • 嗨 Foo L,你能回答这个问题吗stackoverflow.com/questions/43583136/…
    【解决方案4】:

    这是一个可重用的 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)
    

    我迟到了这个问题 - 在这里为另一个问题创建了这个指令。 Simple Angular Directive for Bootstrap Modal

    希望这会有所帮助。

    【讨论】:

    【解决方案5】:

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

    如;

    <button type="button" class="btn btn-default" data-dismiss="modal">Send Form</button>
    

    【讨论】:

      【解决方案6】:

      我已将the answer by @isubuzthis answer by @Umur Kontacı on attribute directives 重新混合到一个版本中,您的控制器不会调用类似DOM 的操作,如“dismiss”,而是尝试更多的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>

      【讨论】:

      • 嗨,Jeroen,你能回答这个问题吗stackoverflow.com/questions/43583136/…
      • @Vinoth Ping 几位随机(或略微相关)其他关于您的问题的帖子的作者对我来说似乎很糟糕。撰写精彩的问题,在找到问题时更新它们并提供更多信息和详细信息,以及赏金系统是吸引帖子关注的更好方式。
      【解决方案7】:
      **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()

      【讨论】:

      • 请考虑在您的代码周围添加一些解释,以及为什么它可以解决问题。很难理解您回答的目的。
      • 嗨,peeyush Singh,你能回答这个问题吗stackoverflow.com/questions/43583136/…
      【解决方案8】:

      你可以用一个简单的 jquery 代码来做。

      $('#Mymodal').modal('hide');
      

      【讨论】:

      • 不是在 jQuery 中,而是在 AngularJS 中
      • 此外,在 Angular 应用程序中应避免使用 jQuery。 DOM 操作应该使用指令来完成。
      • 你可以使用类似的东西,但正如博世所说,它必须在指令中。
      猜你喜欢
      • 2015-04-14
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-07
      相关资源
      最近更新 更多