Twitter bootstrap 从Angular Controller关闭Twitter引导模式
我有一个模式窗口,用于向用户呈现表单。他们输入信息,然后按下一个按钮,然后点击。服务器处理请求并发回响应。当响应成功时,我想从控制器关闭模式窗口。如何做到这一点 模态是另一个页面中包含的部分 主页:Twitter bootstrap 从Angular Controller关闭Twitter引导模式,twitter-bootstrap,angularjs,angularjs-directive,Twitter Bootstrap,Angularjs,Angularjs Directive,我有一个模式窗口,用于向用户呈现表单。他们输入信息,然后按下一个按钮,然后点击。服务器处理请求并发回响应。当响应成功时,我想从控制器关闭模式窗口。如何做到这一点 模态是另一个页面中包含的部分 主页: <!-- main content --> <p>Foo</p> <!-- angular directive --> <foo-directive></foo-directive> 在成功和错误函数中,从控制器关闭模态的正确
<!-- main content -->
<p>Foo</p>
<!-- angular directive -->
<foo-directive></foo-directive>
在成功和错误函数中,从控制器关闭模态的正确方法是什么
提前谢谢,你看了吗?有一个Dialog(ui.bootstrap.Dialog)指令运行得很好。您可以在回调过程中以角度方式关闭对话框(根据示例):
更新:
该指令已被重命名。我们可以在不使用angular ui的情况下实现相同的功能。这可以使用角度指令来完成 首先,将指令添加到模式
<div class="modal fade" my-modal ....>...</div>
现在从控制器中调用disclesh()方法
app.controller('MyCtrl', function($scope, $http) {
// You can call dismiss() here
$scope.dismiss();
});
我仍然是在我的早期与角js。我知道我们不应该在控制器内操纵DOM。所以我在指令中有DOM操作。我不确定这是否同样糟糕。如果我有更好的选择,我会把它贴在这里
需要注意的重要一点是,我们不能简单地在视图中使用ng hide或ng show来隐藏或显示模态。这只是隐藏了模态,而不是模态背景。我们必须调用modal()实例方法来完全删除modal 这里有一个可重用的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>
我来不及回答这个问题了-为另一个问题创建了这个指令
希望这能有所帮助。您可以将数据dismission=“modal”添加到调用angularjs函数的按钮属性中
例如,
<button type="button" class="btn btn-default" data-dismiss="modal">Send Form</button>
发送表单
您可以用一个简单的jquery代码来完成
$('#Mymodal').modal('hide');
您可以这样做:
angular.element('#modal').modal('hide');
我将其重新混合到一个版本中,在这个版本中,控制器不调用类似DOM的操作,如“dismise”,而是尝试更MVVM风格,设置布尔属性isInEditMode
。视图依次将此信息链接到打开/关闭引导模式的属性指令
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();
});
}
}
};
});
var-app=angular.module('myApp',[]);
应用程序指令('myModal',函数(){
返回{
限制:“A”,
作用域:{myModalIsOpen:'='},
链接:功能(范围、元素、属性){
范围.$watch(
函数(){return scope.myModalIsOpen;},
function(){element.modal(scope.myModalIsOpen?'show':'hide');}
);
}
}
});
应用程序控制器('MyCtrl',函数($scope){
$scope.isInEditMode=false;
$scope.toggleEditMode=函数(){
$scope.isInEditMode=!$scope.isInEditMode;
};
});代码>
模态体!IsInEditMode=={{IsInEditMode}}
接近
切换编辑模式
isInEditMode=={{isInEditMode}}
**仅启动引导模式关闭按钮单击事件**
var-app=angular.module('myApp',[]);
app.controller('myCtrl',函数($scope,$http){
$('#btnClose')。单击();//这是触发单击事件的引导模式关闭按钮id
})
--------------------------------------------------------------------------------
模态中的一些文本
<div class="modal fade" my-modal ....>...</div>
接近
只需在我设置btnClose时设置模式“close button”id,对于角度关闭模式,您只需像我设置$(“#btnClose”)一样触发关闭按钮单击事件。单击()您可以提供FooService
的代码吗?非常感谢。请考虑你接受的答案,这样我就可以删除我的。我厌倦了沮丧的心情。:-)关闭对话框会导致对话框中的表单提交。有什么方法可以停止并关闭吗?我觉得angular ui bootstrap在功能和样式方面还没有正式发布的bootstrap功能。Hi Foo L,你能回答这个问题吗?注意,为了让它工作,必须在angular.js
之前加载jquery.js
和bootstrap modal.js
,否则元素对象将不具有modal()
函数。您好!我想在同一页上对两个模态进行此操作。我该怎么做?嘿,这不管用(至少在我的情况下)。如果我们在链接
中添加scope.dislose()
,我认为我们不能从单独的控制器
使用它。我试过你的解决方案,但没用。从视图调用它-例如ng click=“disease();”
可以工作,但是,将它带到不同的控制器
,即使控制器
覆盖了指令
不起作用。@Syl,但是在控制器内访问dom元素$(元素)。modal
是一种好的做法。这不应该在服务中完成吗?Hi isubuz,你能回答这个问题吗?我在寻找一种方法来打开一个基于表达式的模态-这就成功了!谢谢Ender2050,你能回答这个问题吗?不是在jQuery中,而是在AngularJS中。此外,在Angular应用程序中要避免使用jQuery。DOM操作应该用指令来完成。你可以使用类似的东西,但是正如博世所说的,它必须在指令里面。请考虑在代码周围添加一点解释,以及它为什么能解决这个问题。因为很难理解你回答的目的。嗨,peeyush Singh,你能回答这个问题吗嗨,Jeroen,你能回答这个问题吗@Vinoth Ping几位随机(或稍微相关)的作者关于你的问题的其他帖子对我来说似乎很糟糕。创作好的问题,当你完成时,用更多的信息和细节更新它们
<div modal-show modal-visible="showDialog" class="modal fade"> ...bootstrap modal... </div>
$scope.showDialog = false;
$timeout(function () { $scope.showDialog = true; }, 2000)
$timeout(function () { $scope.showDialog = false; }, 5000)
<button type="button" class="btn btn-default" data-dismiss="modal">Send Form</button>
$('#Mymodal').modal('hide');
angular.element('#modal').modal('hide');
**just fire bootstrap modal close button click event**
var app = angular.module('myApp', []);
app.controller('myCtrl',function($scope,$http){
$('#btnClose').click();// this is bootstrap modal close button id that fire click event
})
--------------------------------------------------------------------------------
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" id="btnClose" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>