动态加载引导模态内容并使用 Angular.js 打开它

Dynamically Load Bootstrap Modal Content and Open It Using Angular.js

本文关键字:Angular js 加载 模态 动态      更新时间:2023-09-26

我正在尝试动态加载模态内容并在用户单击按钮时打开它。我已经设法使用 ngInclude 动态加载内容,但之后我无法打开它。我的代码:

<div class="modal-container" id="modal-container" ng-include="modal_template"></div>

和我的控制器.js

function set_modal_template(templateName, callback) {
    $scope.modal_template = templateName;
    callback();
}
$scope.load_modal_sms = function () {
    set_modal_template("/static/partials/modal_template.html", function() {
        $('#modal').modal('show');
    });
};

和我的modal_template.html:

<div class="modal fade" id="modal" role="dialog">
    <div class="modal-dialog" modal-sm>
        <div class="modal-content">
            <div class="modal-header">
                <h4>Pop-up head. Email</h4>
            </div>
            <div class="modal-body">
                <h4>Pop-up body.</h4>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
</div>

正在发生的事情是modal_content.html的内容完美加载,但模态没有显示。如果我再次单击,我只能看到模态后面的透明黑色层,使屏幕变黑,而不是模态本身。

谢谢!

您可以使用多种技术来解决此问题。有一些模块提供模态服务,包括angular-ui,我认为这些模块可以更好地解决您的问题(以及更多)。

角度模态服务

角度模态服务是一个专门用于创建模态的模块。它允许提供模板或模板服务,并支持为模态定义范围(对于抽象很有用)。

您上面的示例代码将是

<div class="modal fade" id="modal" role="dialog">
    <div class="modal-dialog" modal-sm>
        <div class="modal-content">
            <div class="modal-header">
                <h4>Pop-up head. Email</h4>
            </div>
            <div class="modal-body">
                <h4>Pop-up body.</h4>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
</div>
// Do not forget to include the module dependency: var myApp = angular.module('myApp', ['angularModalService']);
myApp.controller('MyController', ['ModalService', function (ModalService) {
    $scope.load_modal_sms = function () {
        var modal = ModalService.showModal({
          templateUrl: "/static/partials/modal_template.html",
          controller: function () { /* controller code */ }
        }).then(function (modal) {
            // Modal has been loaded...
            modal.element.modal();
        });
    };
}]);

Angular-UI-bootstrap

Angular-UI-bootstrap (bower install angular-ui-bootstrap ) 有一个非常容易使用的$modal服务:

<div class="modal fade" id="modal" role="dialog">
    <div class="modal-dialog" modal-sm>
        <div class="modal-content">
            <div class="modal-header">
                <h4>Pop-up head. Email</h4>
            </div>
            <div class="modal-body">
                <h4>Pop-up body.</h4>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
</div>
// Do not forget to include the module dependency: var myApp = angular.module('myApp', ['ui.bootstrap.modal']);
myApp.controller('MyController', ['$modal', function ($modal) {
    $scope.load_modal_sms = function () {
        var modal = $modal.open({
          templateUrl: "/static/partials/modal_template.html",
          scope: { /* custom scope */ }
        });
    };
}]);

使用您的方法

在尝试 .modal() 之前,您可能希望先给 angular 一些时间来加载和渲染加载的模板。如果你没有注意到,你的callback没有做任何特别的事情。您也可以在没有任何回调的情况下运行代码。

<div class="modal fade" id="modal" role="dialog" ng-if="detectLoaded()">
    <div class="modal-dialog" modal-sm>
        <div class="modal-content">
            <div class="modal-header">
                <h4>Pop-up head. Email</h4>
            </div>
            <div class="modal-body">
                <h4>Pop-up body.</h4>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
</div>
function set_modal_template(templateName) {
    $scope.modal_template = templateName;
}
$scope.load_modal_sms = function () {
    set_modal_template("/static/partials/modal_template.html");
};
$scope.detectLoaded = function () {
    // If this function is called, the template has been loaded...
    setTimeout(function () { // Give some time for the template to be fully rendered
        $('#modal').modal('show');
    }, 10);
    return true;
};

您还可以通过更改 ng-if 的位置或在div 之后创建存根元素来消除超时。您可以从 detectLoaded 函数返回 false 以使存根元素不显示。我还没有测试最后一个选项,不知道它是否有效,你必须检查。

<div class="modal fade" id="modal" role="dialog">
    <div class="modal-dialog" modal-sm>
        <div class="modal-content">
            <div class="modal-header">
                <h4>Pop-up head. Email</h4>
            </div>
            <div class="modal-body">
                <h4>Pop-up body.</h4>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
</div>
<div ng-if="detectLoaded()"></div>
$scope.detectLoaded = function () {
    $('#modal').modal('show');
    return false;
};