Bootstrap模态的简单角度指令

Ler*_*eme 6 twitter-bootstrap angularjs

任何人都有一个简单的指令来自动显示Bootstrap模式?在Bootstrap 3中,他们取消了自动显示模态的功能,因此我无法使用角度ng-if show块.任何帮助都会很棒.

war*_*ckh 18

更新角度1.2和Bootstrap 3.1.1:http://embed.plnkr.co/WJBp7A6M3RB1MLERDXSS/

我扩展了Ender2050的答案,因此该指令没有孤立的范围.这意味着模态内容可以包含对范围对象的引用.还要重用指令属性,因此只需要一个属性.

app.directive("modalShow", function ($parse) {
    return {
        restrict: "A",
        link: function (scope, element, attrs) {

            //Hide or show the modal
            scope.showModal = function (visible, elem) {
                if (!elem)
                    elem = element;

                if (visible)
                    $(elem).modal("show");                     
                else
                    $(elem).modal("hide");
            }

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

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

    };
});
Run Code Online (Sandbox Code Playgroud)

用法:

<div modal-show="showDialog" class="modal fade"> ...bootstrap modal... </div>
Run Code Online (Sandbox Code Playgroud)


End*_*050 13

这是一个隐藏和显示Bootstrap模态的Angular指令.

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();
                });

            }

        }
    };

});
Run Code Online (Sandbox Code Playgroud)

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

<div modal-show class="modal fade"> ...bootstrap modal... </div>
Run Code Online (Sandbox Code Playgroud)

用法示例#2 - 它在模态可见属性中使用Angular表达式

<div modal-show modal-visible="showDialog" class="modal fade"> ...bootstrap modal... </div>
Run Code Online (Sandbox Code Playgroud)

另一个示例 - 为了演示控制器交互,您可以向控制器添加这样的内容,它将在2秒后显示模态,然后在5秒后隐藏它.

$scope.showDialog = false;
$timeout(function () { $scope.showDialog = true; }, 2000)
$timeout(function () { $scope.showDialog = false; }, 5000)
Run Code Online (Sandbox Code Playgroud)

我很想知道人们想出了什么其他解决方案.干杯!

  • 这可以工作但是它不会在控制器的范围内转动showDialog,并且在我解除弹出窗口后,相应的属性模态可见 - 再次为false.只有它从DOM中消失,但该更改未反映在模型中.我该怎么办 ? (2认同)