Bootstrapモーダルを自動的に表示する簡単なディレクティブを持っている人はいますか? Bootstrap 3 では、モーダルを自動的に表示する機能が削除されたため、angular ng-if show ブロックを使用できません。どんな助けでも素晴らしいでしょう。
35201 次
2 に答える
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();
});
}
}
};
});
使用例 #1 - これは、モーダルを表示することを前提としています - 条件として ng-if を追加できます
<div modal-show class="modal fade"> ...bootstrap modal... </div>
使用例 #2 - これは modal-visible 属性で 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)
人々が思いつく他の解決策を見たいと思っています。乾杯!
于 2013-10-28T20:36:56.283 に答える