简体   繁体   English

最初没有隐藏angular-ui模态

[英]angular-ui modal is not initially hidden

I'm following this angular recipes page for adding a modal dialog to my ui. 我正在关注这个有角度的食谱页面,用于向我的用户界面添加模式对话框。 It suggests the following markup, which I've added to one of my views. 它建议以下标记,我已将其添加到我的一个视图中。

... html for my view is here ...
<button class="btn" ng-click="open()">Open Modal</button>
<div modal="showModal" close="cancel()">
  <div class="modal-header">
      <h4>Modal Dialog</h4>
      ... etc, from the recipe doc
</div>

What I want to see is my view, plus an "Open Modal" button on the bottom and nothing else. 我想看到的是我的视图,底部有一个“打开模态”按钮,仅此而已。 What I see instead is the button and the content of the modal already visible on the page. 我所看到的是按钮和模式的内容在页面上已经可见。

The very next words in the recipe doc are: 食谱文档中的下一个单词是:

Note that even though we don't specify it explicitly the modal dialog is hidden initially via the modal attribute. 请注意,即使我们没有明确指定,模态对话框也最初是通过modal属性隐藏的。 The controller only handles the button click and the showModal value used by the modal attribute. 控制器仅处理按钮单击和模式属性使用的showModal值。

Why is my modal mark up initially visible on the page? 为什么我的模式标记最初在页面上可见? I think I have installed angular-ui properly... in my index.html: 我想我已经在index.html中正确安装了angular-ui:

<script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>

And in my app JS: 在我的应用JS中:

angular.module('MonteAdmin', [
    ...
    'ui.bootstrap',
    ...
  ])

That recipes page is likely out of date. 该食谱页面可能已过时。 At the time of the writing it might have been possible to pass a variable showModal to the modal directive to reveal or hide it. 在撰写本文时,可能已经将变量showModal传递给modal指令以显示或隐藏它。 In your controller, you would have been able to show the modal by setting the scope variable showModal to true or false: 在控制器中,您可以通过将范围变量showModal设置为true或false来显示模式:

$scope.showModal = false;

$scope.open = function() {
  $scope.showModal = true;
}

The current version does not work that way. 当前版本无法以这种方式工作。 You will have much better experience if you read the official documentation for the library at Angular UI Bootstrap 如果您在Angular UI Bootstrap上阅读了库的官方文档,将会有更好的体验

If you are using the latest version of the library, the directive is no longer modal but uib-modal . 如果使用的是最新版本的库,则伪指令不再是modal而是uib-modal In addition, you have a bit more work to do to implement your modal. 此外,您还需要做更多的工作来实现模态。

Modal markup should be in a script tag, with a type set to text/ng-template as per the official example: 模态标记应在脚本标签中,根据官方示例,其类型设置为text/ng-template

<script type="text/ng-template" id="stackedModal.html">
    <div class="modal-header">
        <h3 class="modal-title" id="modal-title-{{name}}">The {{name}} modal!</h3>
    </div>
    <div class="modal-body" id="modal-body-{{name}}">
        Having multiple modals open at once is probably bad UX but it's technically possible.
    </div>
</script>

To actually open the modal, your button click should trigger the following example function: 要实际打开模态,单击按钮应触发以下示例函数:

var modalInstance = $uibModal.open({
  animation: $ctrl.animationsEnabled,
  ariaLabelledBy: 'modal-title',
  ariaDescribedBy: 'modal-body',
  templateUrl: 'myModalContent.html',
  controller: 'ModalInstanceCtrl',
  controllerAs: '$ctrl',
  size: size,
  appendTo: parentElem,
  resolve: {
    items: function () {
      return $ctrl.items;
    }
  }
});

You must also define a controller for the modal, itself: 您还必须为模式本身定义一个控制器:

angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($uibModalInstance, items) {
  var $ctrl = this;
  $ctrl.items = items;
  $ctrl.selected = {
    item: $ctrl.items[0]
  };

  $ctrl.ok = function () {
    $uibModalInstance.close($ctrl.selected.item);
  };

  $ctrl.cancel = function () {
    $uibModalInstance.dismiss('cancel');
  };
});

All of this code is found on the official documentation for Angular UI Bootstrap 所有这些代码都可以在Angular UI Bootstrap的官方文档中找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM