mahery rafara
mahery rafara

Reputation: 368

Angular-ui bootstrap modal: disappearing after updating model from ajax

I am working on templates using angular ui bootstrap (ui.boostrap.modal). The data {{property}} is created from Ajax requests.

<!-- template for the modal  box-->
    <div><a data-toggle="modal" href="#{{property.domId()}}"
           class="btn btn-default">{{property.label}}</a></div>
        <div id="{{property.domId()}}"
             class="modal fade" role="dialog">
            <div class="modal-dialog">
                <div class="modal-content">
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-12 panel panel-info">

                            <div class="panel-heading">
                        <span tooltip-placement="bottom"
                              tooltip-html-unsafe="{{property.tooltip}}">
                              {{property.label}}</span>
                            </div>
                            <div class="panel-body">
                                <div ng-repeat="property in property.value">
                                        <div ng-include="property.templateUrl">
                                        </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
                </div>
            </div>
        </div>

Some buttons in the modal window updates the model in $scope and so on.

The problem is: After returning from the ajax promise, the DOM start rebuilding the modal box. At some point, deep in $digest, it fails to render an element and the modal window disappear. It leaves only the backdrop shadow (that prevents me to click anything).

 <div class="modal-backdrop fade in"></div>

In the Developer Tools you can see the overall DOM has been completely resolved by Angular.

Upvotes: 1

Views: 2732

Answers (1)

mahery rafara
mahery rafara

Reputation: 368

I'll answer my own questions because it could help later. Actually it is not the way ui-bootstrap uses Bootstrap 3 CSS. The HTML code worked with AngularJS because I am still using bootstrap.js XD. The modal was displayed by Bootstrap3... Here is how it can be done with the same use-case.

JS code: create a open() function to launch the Modal. Should supply a template and a controller. I am passing $scope as the scope because of my specific use-case (don't copy this code as is without understanding scopes)

    $scope.open = function () {
        var modalInstance = $modal.open({
            templateUrl: 'modalContent.html',
            controller: 'ModalInstanceController',
            scope: $scope
        });
    };

}]);

The second controller ModalInstanceController is the one "inside" the modal box. You should inject the scope and the created modal.

testModule.controller('ModalInstanceController',
    ['$scope', '$modalInstance', function ($scope, $modalInstance) {

    $scope.close = function () {
        $modalInstance.close('close');
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

In the partial, you can create an inline template with text/ng-template. The modal is "inside" this script. The last button is opening the modal with the open() from main controller. In standard BS3 you have a button targeting the modal id. Here you need to call the open() method. That's the main workaround you have to perform in your HTML.

<div>
    <script type="text/ng-template" id="modalContent.html">
        <div class="modal-header">{{property.label}}
        </div>
        <div class="modal-body">
            <div class="row">
                <div class="col-md-12 panel panel-info">

                    <div class="panel-heading">
                <span  tooltip-placement="bottom"
                      tooltip-html-unsafe="{{property.tooltip}}">
                      {{property.label}}</span>
                    </div>
                    <div class="panel-body">
                        <div ng-repeat="property in property.value">
                            <div ng-include="property.templateUrl">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" ng-click="close()">OK</button>
            <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
        </div>
    </script>

    <button class="btn btn-default" ng-click="open()">Open</button>
</div>

Upvotes: 3

Related Questions