首页 > 解决方案 > Angular UI Boostrap:如何在没有控制器的情况下关闭模式?

问题描述

我是 AngularJS 的新手,刚刚经历了 Angular UI Modal 中描述的:http ://angular-ui.github.io/bootstrap/ 。

我想创建一个简单的模式,只有一个按钮来关闭模式。如何在不为模态创建单独的控制器的情况下做到这一点?模态模板中是否有ng-click事件脚本来完成这项工作?比如this.close()...

我想要达到的效果是这样的:

模板:

<div class="modal-header">
  <h3>Modal header</h3>
</div>
<div class="modal-body">
 <h4>Just something random here</h4>
</div>
<div class="modal-footer">
    <button class="btn btn-primary" ng-click="this.close()">OK</button>
</div>

页面控制器:

  $scope.openModal = function() {
    $uibModal.open({
      templateUrl: "modalContent.html",
      // it's so simple so that I don't want a separate controller
      //controller: "ModalContentCtrl",
      size: '',
      backdrop: 'static',
      keyboard: false
    });
  };

标签: javascriptangularjsangular-ui-bootstrap

解决方案


文档

与模态内容相关的范围增加了:

$close(result) (Type: function) - 一种可用于关闭模式并传递结果的方法。

$dismiss(reason) (Type: function) - 一种可以用来关闭模式的方法,传递一个原因。

这些方法可以轻松关闭模式窗口,而无需创建专用控制器。

所以这有效:

<div class="modal-header">
  <h3>Modal header</h3>
</div>
<div class="modal-body">
 <h4>Just something random here</h4>
</div>
<div class="modal-footer">
    <button class="btn btn-primary" ng-click="$close()">OK</button>
</div>

推荐阅读