angularjs - 如何在 Angular UI Bootstrap 中查看模态是否打开

标签 angularjs angular-ui-bootstrap

我正在使用 Angular UI Bootstrap。如果模态打开,我想显示 true 。如果它没有打开,我想显示 false 。有没有办法在 HTML 中做到这一点?

我尝试使用以下代码,但它是错误的:

<code>myForm.$modalStack.opened={{myForm.$modalStack.opened}}</code>

关于如何正确执行此操作的任何想法?

在我用来触发模态的相关代码下方:

HTML:
<button ng-click="myForm.agreement()">

Controller 中的代码:
.controller('MyFormCtrl',
  function ($location, $stateParams, $modal, $scope, $http) {
    var myForm = this;
    // . . . 

   myForm.agreement = agreement;

   function agreement() {

       $modal.open({

          templateUrl: 'views/agreement.html'
  })
});

最佳答案

opened 返回的 $modal.open 属性是一个可以 Hook 的 promise 。

因此,使用他们的示例,请参见此处 - http://plnkr.co/edit/PsEqTIy8CDUU88HMLxbC?p=preview

$scope.modalOpen = false;
$scope.open = function (size) {
    var modalInstance =  $modal.open({
        animation: $scope.animationsEnabled,
        templateUrl: 'myModalContent.html',
        controller: 'ModalInstanceCtrl',
        size: size,
        resolve: {
            items: function () {
                return $scope.items;
            }
        }
    });

    modalInstance.opened.then(function () {
        $scope.modalOpen = true;
    });

    // we want to update state whether the modal closed or was dismissed,
    // so use finally to handle both resolved and rejected promises.
    modalInstance.result.finally(function (selectedItem) {
        $scope.modalOpen = false;
    });
};

你想调用 promise ,然后做任何你需要的事情。 .opened 是模式打开时的 promise ,.result 是模式关闭时的 promise 。所以使用这个想法,你可以使用 $scope.modalOpen 作为你的 bool 值。

关于angularjs - 如何在 Angular UI Bootstrap 中查看模态是否打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31594640/

相关文章:

php - 如何使用 AngularJS 或 jQuery 更改 HTML 表格中的文本颜色?

angularjs - UI Bootstrap anchor 在 Accordion 标题中不起作用

javascript - Bootstrap UI 轮播功能不会在选项卡内运行

angularjs - 从 Angular JS 中的指令调用 Controller 函数

angularjs - 如何判断在 Angular-UI 中选择了哪个 Bootstrap 选项卡

javascript - 将整个对象传递给 ng-grid 中的 cellFilter 函数

javascript - 从index.html javascript调用angularJS Controller 函数

javascript - 如何从 $httpProvider 拦截器的 'reponse' 函数抛出错误

javascript - 抛出自定义错误时触发 $stateChangeError

javascript - Angular-UI typeahead 结果未显示在下拉列表中