任何人都有一个简单的指令来自动显示Bootstrap模式?在Bootstrap 3中,他们取消了自动显示模态的功能,因此我无法使用角度ng-if show块。任何帮助都会很棒。
答案 0 :(得分: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();
});
}
};
});
用法:
<div modal-show="showDialog" class="modal fade"> ...bootstrap modal... </div>
答案 1 :(得分:13)
这是一个Angular指令,它将隐藏并显示一个Bootstrap模态。
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)
我很想知道人们提出了哪些其他解决方案。干杯!