ionic モーダルウィンドウ


$ionicModal

$ionicModal はユーザーのメインインターフェースを覆うことができるコンテンツボックスです。

index ファイルまたは他のファイル内に以下のコードを埋め込むことができます(内部のコードは自分のビジネスシナリオに応じて変更できます)。

<script id="my-modal.html" type="text/ng-template">
  <ion-modal-view>
    <ion-header-bar>
      <h1 class="title">My Modal title</h1>
    </ion-header-bar>
    <ion-content>
      Hello!
    </ion-content>
  </ion-modal-view>
</script>

そして、Controller 内で $ionicModal を注入します。その後、先ほど作成したテンプレートを呼び出して初期化操作を行います。以下のコードのようになります:

angular.module('testApp', ['ionic'])
.controller('MyController', function($scope, $ionicModal) {
  $ionicModal.fromTemplateUrl('my-modal.html', {
    scope: $scope,
    animation: 'slide-in-up'
  }).then(function(modal) {
    $scope.modal = modal;
  });
  $scope.openModal = function() {
    $scope.modal.show();
  };
  $scope.closeModal = function() {
    $scope.modal.hide();
  };
  //Cleanup the modal when we're done with it!
  $scope.$on('$destroy', function() {
    $scope.modal.remove();
  });
  // Execute action on hide modal
  $scope.$on('modal.hidden', function() {
    // Execute action
  });
  // Execute action on remove modal
  $scope.$on('modal.removed', function() {
    // Execute action
  });
});

メソッド

fromTemplate(templateString, options)
パラメータ 型 詳細
templateString 文字列

テンプレートの文字列をモーダルウィンドウのコンテンツとして使用します。

options オブジェクト

options は ionicModal#initialize メソッドに渡されます。

戻り値:オブジェクト。ionicModal コントローラーのインスタンスです。

fromTemplateUrl(templateUrl, options)
パラメータ 型 詳細
templateUrl 文字列

テンプレートのURLを読み込みます。

options オブジェクト

ionicModal#initialize メソッドを介してオブジェクトを渡します。

戻り値:promise オブジェクト。Promises オブジェクトは CommonJS ワーキンググループが提唱した仕様であり、非同期プログラミングに統一されたインターフェースを提供することを目的としています。


ionicModal

$ionicModal サービスによってインスタンス化されます。

ヒント:各モジュールのクリアが完了したら、メモリリークを防ぐために remove() メソッドを必ず呼び出してください。

注意:モジュールは初期スコープから 'modal.shown' と 'modal.hidden' をブロードキャストし、自身をパラメータとして渡します。

メソッド

initialize(可选)

新しいモーダルウィンドウコントローラーのインスタンスを作成します。

パラメータ 型 詳細
options オブジェクト

オプションオブジェクトには以下のプロパティがあります:

  • {object=} 範囲子スコープ。デフォルト:$rootScope の子スコープを作成します。
  • {string=} アニメーション表示または非表示のアニメーション。デフォルト:'slide-in-up'
  • {boolean=} 最初の入力ボックスがフォーカスを取得する表示時に、モーダルウィンドウの最初の入力要素が自動的にフォーカスを取得するかどうか。デフォルト:false。
  • {boolean=}backdropClickToClose` 背景をクリックしたときにモーダルウィンドウを閉じるかどうか。デフォルト:true。
show()

モーダルウィンドウのインスタンスを表示します。

  • 戻り値:promisepromise オブジェクト。モーダルウィンドウのアニメーション完了後に解決されます。
hide()

モーダルウィンドウを非表示にします。

  • 戻り値:promisepromise オブジェクト。モーダルウィンドウのアニメーション完了後に解決されます。
remove()

DOM からモーダルウィンドウのインスタンスを削除してクリーンアップします。

  • 戻り値:promisepromise オブジェクト。モーダルウィンドウのアニメーション完了後に解決されます。
isShown()
  • 戻り値:ブール値。モーダルウィンドウが表示されているかどうかを判断します。

例

HTML コード

<html ng-app="ionicApp">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> 
    
    <title>サンプル(example.com)</title>
    <link href="../static/ionic/css/ionic.min.css" rel="stylesheet">
    <script src="../static/ionic/js/ionic.bundle.min.js"></script>
  </head>
  <body ng-controller="AppCtrl">
    
    <ion-header-bar class="bar-positive">
      <h1 class="title">Contacts</h1>
      <div class="buttons">
        <button class="button button-icon ion-compose" ng-click="modal.show()">
        </button>
      </div>
    </ion-header-bar>
    <ion-content>
      <ion-list>
        <ion-item ng-repeat="contact in contacts">
          {{contact.name}}
        </ion-item>
      </ion-list>
    </ion-content>
    
    <script id="templates/modal.html" type="text/ng-template">
      <ion-modal-view>
        <ion-header-bar class="bar bar-header bar-positive">
          <h1 class="title">New Contact</h1>
          <button class="button button-clear button-primary" ng-click="modal.hide()">Cancel</button>
        </ion-header-bar>
        <ion-content class="padding">
          <div class="list">
            <label class="item item-input">
              <span class="input-label">First Name</span>
              <input ng-model="newUser.firstName" type="text">
            </label>
            <label class="item item-input">
              <span class="input-label">Last Name</span>
              <input ng-model="newUser.lastName" type="text">
            </label>
            <label class="item item-input">
              <span class="input-label">Email</span>
              <input ng-model="newUser.email" type="text">
            </label>
            <button class="button button-full button-positive" ng-click="createContact(newUser)">Create</button>
          </div>
        </ion-content>
      </ion-modal-view>
    </script>
    
  </body>
</html>

CSS コード

body {
  cursor: url('http://www.example.com/try/demo_source/finger.png'), auto;
}

JavaScript コード

angular.module('ionicApp', ['ionic'])

.controller('AppCtrl', function($scope, $ionicModal) {
  
  $scope.contacts = [
    { name: 'Gordon Freeman' },
    { name: 'Barney Calhoun' },
    { name: 'Lamarr the Headcrab' },
  ];

  $ionicModal.fromTemplateUrl('templates/modal.html', {
    scope: $scope
  }).then(function(modal) {
    $scope.modal = modal;
  });
  
  $scope.createContact = function(u) {        
    $scope.contacts.push({ name: u.firstName + ' ' + u.lastName });
    $scope.modal.hide();
  };

});

その他の拡張