简体   繁体   English

ui-bootstrap 模态不会关闭或关闭

[英]ui-bootstrap modal won't close or dismiss

New to ui-bootstrap and I've gotten the modal to appear, but can't get it to dismiss with a button press. ui-bootstrap 的新手,我已经让模式出现,但无法通过按下按钮将其关闭。 I've tried a many combinations but nothing seems to work (or it makes the modal not appear at all).我尝试了很多组合,但似乎没有任何效果(或者它使模态根本不出现)。

The modal is in a separate file as:模态在一个单独的文件中:

<div ng-controller = 'entryCtrl' class="modal" id="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header"></div>
            <div class="modal-body">
                <div class="modal-inner-content">
                </div>
                <div class="modal-footer" id="modal-footer">
                    <button type="button" class="btn btn-success" ng-click="cancel()"><i class="glyphicon glyphicon-backward"></i> Back</button>
                    <button type="button" class="btn btn-danger" ng-click="ok()"><i class="glyphicon glyphicon-forward"></i> Continue</button>
                </div>
            </div>
        </div>
    </div>
</div>

It's called in a different html file with the same controller (I tried having them as different controllers, but then variables wouldn't appear to inject at all, ie the modal instance would be undefined).它在具有相同 controller 的不同 html 文件中调用(我尝试将它们作为不同的控制器,但变量似乎根本不会注入,即模态实例将未定义)。

In the controller, I can get the thing to open as:在 controller 中,我可以打开这个东西:

xControllers.controller('entryCtrl', [
        '$scope', '$window', '$modal', '$log',
        'SharedProperties',
        function ($scope, $window, $modal, $log,
        SharedProperties) {

            $scope.open = function (size) {
                $scope.modal = $modal({
                    templateUrl: './views/modals/possible_slurs.html',
                    controllerAs: ['$window', '$log', '$modalInstance', function ($window, $log, $modalInstance) {
                        this.ok = function () {
                            $modalInstance.close();
                        };

                        this.cancel = function () {
                            $modalInstance.dismiss('cancel');
                            $window.history.back();
                        };
                                    }],
                    scope: $scope
                });
            };

    }]);

For some reason the $modal.open() doesn't work, and template , and controller parameters don't either (it only accepts templateUrl and controllerAs ).由于某种原因, $modal.open()不起作用,并且templatecontroller参数也不起作用(它只接受templateUrlcontrollerAs )。

I attempted to have controllerAs set as a different controller, but then that separate controller wouldn't recognize the modal instance (it came up as undefined).我试图将controllerAs设置为不同的 controller,但随后单独的 controller 将无法识别模态实例(它出现为未定义)。

I have my dependencies set as:我将依赖项设置为:

var x = angular.module('x-app', [
        'xControllers',
        'ngAnimate',
        'ngRoute',
        'ngResource',
        'mgcrea.ngStrap',
        'angularUtils.directives.dirPagination',
        'ui.bootstrap'
    ]);

And their versions:以及他们的版本:

"dependencies": {
    "socket.io": "^2.2.0",
    "angular": "^1.7.8",
    "angular-animate": "^1.7.8",
    "angular-motion": "^0.4.4",
    "angular-resource": "^1.7.8",
    "angular-strap": "^2.3.12",
    "bootstrap": "^4.3.1",
    "jquery": "^3.4.1",
    "angular-bootstrap": "^2.5.0"
  }

Thank you so much and please let me know if more information is needed to help with this!非常感谢,如果需要更多信息来帮助解决这个问题,请告诉我!

This is the most convoluted thing I've ever come across.这是我遇到过的最令人费解的事情。 After several hours and days going back and forth in documentation, I've found the answer: which is, of course, that there were many, many problems going on and the fact that anything was showing up at all was a total fluke.在文档中反复数小时和数天后,我找到了答案:当然,有很多很多问题正在发生,而任何事情都出现的事实完全是侥幸。

I'll just post what I got working, but suffice it to say: the Angular version is very important here.我只会发布我的工作内容,但我只想说:Angular 版本在这里非常重要。

The new working modal (notice that there is no controller specified here nor any of those wrapping div s):新的工作模式(注意这里没有指定 controller 也没有任何包装div s):

<div class="modal-header"></div>
<div class="modal-body">
    <div class="modal-inner-content">
    </div>
    <div class="modal-footer" id="modal-footer">
        <button type="button" class="btn btn-success" ng-click="cancel()"><i class="glyphicon glyphicon-backward"></i> Back</button>
        <button type="button" class="btn btn-danger" ng-click="ok()"><i class="glyphicon glyphicon-forward"></i> Continue</button>
    </div>
</div>

The new working controller which opens the modal (note the switch to $uibModal ):新的工作 controller 打开模态(注意切换到$uibModal ):

xControllers.controller('entryCtrl', [
        '$scope', '$window', '$uibModal', '$log',
        'SharedProperties',
        function ($scope, $window, $uibModal, $log,
        SharedProperties) {

            $scope.open = function (size) {
                $uibModal.open({
                    templateUrl: './views/modals/modal.html',
                    controller: 'modalInstanceCtrl',
                    scope: $scope
                });
            };

    }]);

And the new modal controller called within that controller:而新的模态 controller 在 controller 中调用:

xControllers.controller('modalInstanceCtrl', [
        '$scope', '$window', '$uibModalInstance',
        function ($scope, $window, $uibModalInstance) {

        $scope.ok = function () {
            $uibModalInstance.close();
        };

        $scope.cancel = function () {
            $uibModalInstance.close();
            $window.history.back();
        };


    }]);

And that seemed to do the trick.这似乎起到了作用。 A large thanks to whoever created this Plunker ( http://embed.plnkr.co/4VcNom/ ) because the official documentation was essentially the opposite of useful in this regard ( https://angular-ui.github.io/bootstrap/ ; it only handles the case where a modal's HTML is loaded in the same HTML doc as the main HTML, not for separate files). A large thanks to whoever created this Plunker ( http://embed.plnkr.co/4VcNom/ ) because the official documentation was essentially the opposite of useful in this regard ( https://angular-ui.github.io/bootstrap/ ; 它只处理模态的 HTML 加载到与主 HTML 相同的 HTML 文档中的情况,而不是单独的文件)。

Anywho, hope this helps others.任何人,希望这对其他人有所帮助。

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

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