如何确定ons-modal显示的状态?

时间:2015-07-09 15:54:45

标签: onsen-ui

当应用程序获取某些数据时,我正在使用ons-modal来显示加载文本和微调器图标。 代码如下:

<ons-modal var="loadingModal">
  <ons-icon icon="ion-load-c" spin="true"></ons-icon>
  <br><br>
  Cargando...
  <br>
</ons-modal>

我可以使用loadingModal.show();loadingModal.hide();

正确显示隐藏信息

但是我如何知道Angular中是否显示或隐藏了它?

1 个答案:

答案 0 :(得分:5)

更新

显然,我不那么优雅的解决方案毕竟不是那么优雅:D

Here is a pull request that shows the method isShown() that should be available soon I guess

在内部,该功能看起来类似于本回答中的内容

isShown() {
  return this.style.display !== 'none';
}

不是一个超级优雅的解决方案,但它有效

if( $scope.loadingModal._element.css('display') == 'none'){
  // hidden now
}else{
  // visible now
}

<!doctype html>
<html lang="en" ng-app="myApp">
  <head>
    <meta charset="utf-8">
    <title>Modal | Onsen UI</title>
    <link rel="stylesheet" href="https://rawgit.com/OnsenUI/OnsenUI/master/demo/styles/app.css"/>
    <link rel="stylesheet" href="https://rawgit.com/OnsenUI/OnsenUI/master/build/css/onsenui.css">
    <link rel="stylesheet" href="https://rawgit.com/OnsenUI/OnsenUI/master/build/css/onsen-css-components.css">

    <script src="https://rawgit.com/OnsenUI/OnsenUI/master/build/js/angular/angular.js"></script>
    <script src="https://rawgit.com/OnsenUI/OnsenUI/master/build/js/onsenui.js"></script>
    <script src="https://rawgit.com/OnsenUI/OnsenUI/master/demo/app.js"></script>
    <script>
      function check($el){
        return $el.css('display') === 'none' ? 'hidden' : 'visible';
      }
      angular.module('myApp').controller('PageController', function($scope) {
        $scope.open = function() {
          $scope.app.modal.show();
          alert(check($scope.app.modal._element));
          setTimeout(function() {
            $scope.app.modal.hide();
            alert(check($scope.app.modal._element));
          }, 2000);
        };
      });
    </script>
  </head>

  <body ng-controller="PageController">

    <ons-navigator>

      <ons-modal var="app.modal">
        <ons-icon icon="ion-load-c" spin="true"></ons-icon>
        <br><br>
        Cargando...
        <br>
      </ons-modal>

      <ons-toolbar>
        <div class="center">Modal</div>
      </ons-toolbar>

      <p style="text-align: center">
        <ons-button modifier="light" ng-click="open();">Open Modal</ons-button>
      </p>

    </ons-navigator>

  </body>
</html>