从控制器触发模态弹出
trigger modal popup from controller
我有一个 bootstrap 模态弹出窗口,其中填充了来自模态控制器的数据。在我用数据填充它之后,我想展示它。为了能够在进入页面时立即显示 modalpopup,我想直接触发它。现在我有一个按钮可以执行此操作,但我怎样才能以正确的方式自动执行此操作?
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog" ng-init="getAllItems()">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<div class="">
<form class="form-horizontal" name="form" role="form">
<div ng-repeat="item in items">
<input type="radio" name="fundselector" ng-model="item" ng-value="item"/> {{item}} <br />
</div>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
我的控制器:
angular.module("Modal")
.controller("ModalController",
[
"$scope", "$rootScope", "$location", "ModalService", "AuthenticationService",
function ($scope, $rootScope, $location, ModalService, AuthenticationService) {
AuthenticationService.GetCurrentWindowsUser(function(username) {
$scope.username = username;
});
$scope.getAllItems = function () {
AuthenticationService.GetCurrentWindowsUser(function (username) {
if (username) {
AuthenticationService.GetItems(username, function (items) {
$scope.items = items;
//Trigger modalpopup here
});
}
});
}
}
]);
尝试这样的事情 -
$('#yourModalId').modal('show');
不要用jquery,用angular-ui.
https://angular-ui.github.io/bootstrap/#/modal
您可以像下面这样以编程方式打开模式,并在解析中传递您的数据。这是直接从他们的示例中复制和粘贴的。
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size,
resolve: {
items: function () {
return $scope.items;
}
}
});
为了修正你的例子,我创建了一个服务来打开模态并将它附加到外部控制器和模态控制器。我不得不从模态中删除 "modal" class 因为它有一个 display:none
属性。 Here 是一个 fiddle,它显示了我所做的事情。但是,@mgiesa 的回答更好。
通过angular.element查找元素并隐藏。
var popup = angular.element("#modal-default");
//for hide model
popup.modal('hide');
//for show model
popup.modal('show');
我有一个 bootstrap 模态弹出窗口,其中填充了来自模态控制器的数据。在我用数据填充它之后,我想展示它。为了能够在进入页面时立即显示 modalpopup,我想直接触发它。现在我有一个按钮可以执行此操作,但我怎样才能以正确的方式自动执行此操作?
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog" ng-init="getAllItems()">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<div class="">
<form class="form-horizontal" name="form" role="form">
<div ng-repeat="item in items">
<input type="radio" name="fundselector" ng-model="item" ng-value="item"/> {{item}} <br />
</div>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
我的控制器:
angular.module("Modal")
.controller("ModalController",
[
"$scope", "$rootScope", "$location", "ModalService", "AuthenticationService",
function ($scope, $rootScope, $location, ModalService, AuthenticationService) {
AuthenticationService.GetCurrentWindowsUser(function(username) {
$scope.username = username;
});
$scope.getAllItems = function () {
AuthenticationService.GetCurrentWindowsUser(function (username) {
if (username) {
AuthenticationService.GetItems(username, function (items) {
$scope.items = items;
//Trigger modalpopup here
});
}
});
}
}
]);
尝试这样的事情 -
$('#yourModalId').modal('show');
不要用jquery,用angular-ui.
https://angular-ui.github.io/bootstrap/#/modal
您可以像下面这样以编程方式打开模式,并在解析中传递您的数据。这是直接从他们的示例中复制和粘贴的。
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size,
resolve: {
items: function () {
return $scope.items;
}
}
});
为了修正你的例子,我创建了一个服务来打开模态并将它附加到外部控制器和模态控制器。我不得不从模态中删除 "modal" class 因为它有一个 display:none
属性。 Here 是一个 fiddle,它显示了我所做的事情。但是,@mgiesa 的回答更好。
通过angular.element查找元素并隐藏。
var popup = angular.element("#modal-default");
//for hide model
popup.modal('hide');
//for show model
popup.modal('show');