Angular-ui: 是否打开了任何模态?
Angular-ui: Is any modal opened?
如果有任何模式打开,我想启动一个代码。通常我想要这样的东西:
$scope.$watch(function () {
return $modal.isOpenState;
}, function (val) {
//my code here
}, true
);
但我不知道该看什么。是的,我可以检测每个实例的打开事件,例如:
modalInstance.opened.then(function() {
//my code here
});
但这不是干的。
P.S。我也可以在 $watch
函数中制作类似 $('.modal').hasClass('in')
的东西,但这有点难看
P.P.S 顺便说一句,我正在使用 ui-router 打开模式(参见常见问题 here)
$modal.open({
templateUrl: "...",
resolve: {... },
controller: function($scope) { ... }
}).result.finally(function() {
//can put code here, but same issue
});
有一个名为 $modalStack
. This service is used by $modal
and has method called getTop
的内部服务 UI 模态使用来检索当前打开的模态实例。因此,您可以注入此服务并在 $rootScope
上简单地观察 getTop
结果。例如:
app.run(function($rootScope, $modalStack) {
$rootScope.$watch($modalStack.getTop, function(newVal, oldVal) {
if (newVal) {
console.log('opened', newVal, oldVal);
}
});
});
如果有任何模式打开,我想启动一个代码。通常我想要这样的东西:
$scope.$watch(function () {
return $modal.isOpenState;
}, function (val) {
//my code here
}, true
);
但我不知道该看什么。是的,我可以检测每个实例的打开事件,例如:
modalInstance.opened.then(function() {
//my code here
});
但这不是干的。
P.S。我也可以在 $watch
函数中制作类似 $('.modal').hasClass('in')
的东西,但这有点难看
P.P.S 顺便说一句,我正在使用 ui-router 打开模式(参见常见问题 here)
$modal.open({
templateUrl: "...",
resolve: {... },
controller: function($scope) { ... }
}).result.finally(function() {
//can put code here, but same issue
});
有一个名为 $modalStack
. This service is used by $modal
and has method called getTop
的内部服务 UI 模态使用来检索当前打开的模态实例。因此,您可以注入此服务并在 $rootScope
上简单地观察 getTop
结果。例如:
app.run(function($rootScope, $modalStack) {
$rootScope.$watch($modalStack.getTop, function(newVal, oldVal) {
if (newVal) {
console.log('opened', newVal, oldVal);
}
});
});