简体   繁体   中英

AngularJS ui.bootstrap modal dialog is not showing

I have already seen this question: AngularJS bootstrap.ui modal not showing . But any of those answers didn't help me.

I'm trying to display modal dialog using ui.bootstrap module. This is my index.html page

<!doctype html>
<html lang="en" ng-app="app">

    <head>
        <title>Title</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
    </head>

    <body ng-controller="Controller">
        <button type="button" class="btn btn-info" ng-click="open()">Open</button>

        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
        <script src="lib/angular.js"></script>
        <script src="lib/ui-bootstrap-tpls.js"></script>
        <script>
            angular
                .module("app", ["ui.bootstrap"])
                .controller("Controller", function ($scope, $uibModal) {
                    $scope.open = function () {
                        $uibModal.open({
                            ariaLabelledBy: 'modal-title',
                            ariaDescribedBy: 'modal-body',
                            templateUrl: 'modal.template.html',
                            controller: 'ModalController',
                            resolve: {
                                users: function () {
                                    return [
                                        { id: 1, name: "aaa" },
                                        { id: 2, name: "bbb" },
                                        { id: 3, name: "ccc" }
                                    ];
                                }
                            }
                        }).result.then(
                            function (result) {
                                console.log(result);
                            },
                            function () {
                                console.log("Modal dialog dismissed!");
                            }
                        );
                    }
                })
                .controller("ModalController", function ($scope, $uibModalInstance) {
                    $scope.ok = function () {
                        $uibModalInstance.close("Modal dialog successfully closed!");
                    }

                    $scope.cancel = function () {
                        $uibModalInstance.dismiss("cancel");
                    }
                });
        </script>
    </body>

</html>

modal.template.html

<div class="modal-header">
    <h3 class="modal-title" id="modal-title">Modal Title</h3>
</div>
<div class="modal-body" id="modal-body">
    <ul>
        <li ng-repeat="user in users">{{user}}</li>
    </ul>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-primary" ng-click="ok()">Ok</button>
    <button type="button" class="btn btn-light" ng-click="cancel()">Cancel</button>
</div>

I don't see any errors in the console, but the modal dialog is not showing.

There are some issues in your code:

  1. Move all <script> tags into head section.
  2. ui-bootstrap-tpl is compatible with bootstrap 3, so use v3.3.7
  3. If you resolve something, it means that you can access it in your controller, but it's not assigned to $scope . So inject users into your controller and assign its value.

     .controller("Controller", function ($scope, $uibModal, users) { $scope.users = users; /* rest of code*/ } 

You can see result of these changes in this plunker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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