角度模态模板不显示

时间:2013-10-03 18:10:20

标签: templates angularjs modal-dialog twitter-bootstrap-3

我对Angular有些新意。我正在尝试在检测到无效的用户角色时显示Bootstap 3模式对话框。我无法显示我的模态模板。这种行为似乎有效,即我可以忽略褪色的叠加......我只是看不到实际的模态模板。

Bootstrap 3 AngularJS 1.0.7 AngularJS UI Bootstrap 0.6.0

控制器

gsApp.controller('MainController', ['$rootScope', '$scope', '$q', '$window', '$location',     '$modal', 'ApplicationCache', 'UserService',
function MainController($rootScope, $scope, $q, $window, $location, $modal, ApplicationCache, UserService) {

   $scope.userRole = "BadRole";

    $scope.showBadRoleModel = function () {
        var showBadRoleModelInstance = $modal.open({
            templateUrl: "badRoleModal.html",
            backdrop: true,
            windowClass: 'modal',
            controller: badRoleModalInstance,
            resolve: {
                items: function () {
                    return $scope.userRole;
                }
            }
        });
    }

    var badRoleModalInstance = function($scope, $modalInstance, items){
        $scope.ok = function () {
            $modalInstance.close();
        };

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

HTML

<div class="row" ng-controller="MainController">

            <script type="text/ng-template" id="badRoleModal.html">
                <div class="modal-header">
                    <h3>I'm a modal!</h3>
                </div>
                <div class="modal-body">
                    <h2>body</h2>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-primary" ng-click="ok()">OK</button>
                    <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
                </div>
            </script>

            <button class="btn" ng-click="showBadRoleModel()">Show bad role modal</button>
        </div>

2 个答案:

答案 0 :(得分:2)

AngularJs UI Bootstrap尚未与Bootstrap 3一起使用。

在此处查看更多详情:https://github.com/angular-ui/bootstrap/issues/331

答案 1 :(得分:0)

这是一个可重用的Angular指令,它将隐藏并显示Bootstrap 3(或2.x)模态。

app.directive("modalShow", function () {
    return {
        restrict: "A",
        scope: {
            modalVisible: "="
        },
        link: function (scope, element, attrs) {

            //Hide or show the modal
            scope.showModal = function (visible) {
                if (visible)
                {
                    element.modal("show");
                }
                else
                {
                    element.modal("hide");
                }
            }

            //Check to see if the modal-visible attribute exists
            if (!attrs.modalVisible)
            {

                //The attribute isn't defined, show the modal by default
                scope.showModal(true);

            }
            else
            {

                //Watch for changes to the modal-visible attribute
                scope.$watch("modalVisible", function (newValue, oldValue) {
                    scope.showModal(newValue);
                });

                //Update the visible value when the dialog is closed through UI actions (Ok, cancel, etc.)
                element.bind("hide.bs.modal", function () {
                    scope.modalVisible = false;
                    if (!scope.$$phase && !scope.$root.$$phase)
                        scope.$apply();
                });

            }

        }
    };

});

用法示例#1 - 这假设您要显示模态 - 您可以添加ng-if作为条件

<div modal-show class="modal fade"> ...bootstrap modal... </div>

用法示例#2 - 它在模态可见属性

中使用Angular表达式
<div modal-show modal-visible="showDialog" class="modal fade"> ...bootstrap modal... </div>

另一个例子 - 为了演示控制器交互,你可以向你的控制器添加这样的东西,它会在2秒后显示模态,然后在5秒后隐藏它。

$scope.showDialog = false;
$timeout(function () { $scope.showDialog = true; }, 2000)
$timeout(function () { $scope.showDialog = false; }, 5000)

我迟迟没有为这个问题做出贡献 - 为另一个问题创建了这个指令。以下是一些相关链接:Simple Angular Directive for Bootstrap Modalhttps://stackoverflow.com/a/19668616/1009125

希望这有帮助。