0

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.

2
  • Move all script tags into head section Commented Sep 28, 2018 at 12:39
  • @barbsan didn't help Commented Sep 28, 2018 at 12:54

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it works now. But I have one more question: can I use Bootstrap v4 instead of v3?
The problem was, as I understood correctly, in inconsistence between Bootstrap v4 and UI Bootstrap for AngularJS. Is there a way to overcome this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.