2016-05-07 8 views
1

私のコントローラには、2つのモーダルが起動しています。そのうちの1つのコードは、モーダルコードをIonicでどこに移動する必要がありますか?

function setUpEditCommentModal() { 
     $ionicModal.fromTemplateUrl('app/recent/edit-comment-modal.html', { 
      scope: $scope, // so close methods available. 
      animation: 'fade-in-scale' 
     }).then(function (modal) { 
      $scope.editCommentModal = modal; 
     }); 

     $scope.closeEditCommentModal = function() { 
      $scope.editCommentModal.hide(); 
     }; 

     $scope.returnFromSavingCommentInModal = function() { 
      var modifiedComment = commentSelector.getComment(); 
      vm.selectedComment.Comment = modifiedComment.Comment; // just note part. 
      $scope.closeEditCommentModal(); 
     }; 

     //Cleanup the modal when we're done with it! 
     $scope.$on('$destroy', function() { 
      $scope.editCommentModal.remove(); 
     }); 

     // Execute action on hide modal 
     $scope.$on('modal.hidden', function() { 
      // Execute action 
      $ionicListDelegate.closeOptionButtons(); 
     }); 

     // Execute action on remove modal 
     $scope.$on('modal.removed', function() { 
      // Execute action 
     }); 
    } 

であり、他のモーダルコードはさらに長くなります。 私のコントローラクラスがそれほど大きくないように、このコードをリファクタリングする最良の方法は何ですか?

このコードをサービスに移行する必要がありますか?

答えて

1

ModalServiceは、register()メソッドを作成できます。これは、一度に複数のモーダルを登録し、約束を返すことができます。プロミスの再解決された値は、ローカルに保存することも、サービスからの参照によって使用することもできます。参照を介して使用するには、現在アクティブなすべてのモーダルのリストをサービスに保存する必要があります。サービスは以下のようになります:https://jsbin.com/busepey/2/edit?html,js,output

.factory('ModalService', function ($ionicModal) { 
    return { 
    register: register 
    }; 

    // TODO: Add an array to store list of all active modals 
    // and use it to manipulate the modal `open()`, `close()` methods 

    function register (config) { 
    return $ionicModal.fromTemplateUrl(config.templateUrl, { 
     scope: config.scope, 
     animation: config.animation 
    }); 
    } 
}); 

を上記の工場を使用して、コントローラで2つのモーダルのためのシンプルな実装は、このJSBinに存在しています。さらに、アクティブなモーダルのリストをサービス内のローカル配列に実装し、新しい/削除されたモーダルに基づいて追加または削除することができます。

UPDATE:モーダルを表示するために、再利用可能な方法のためのサービス:https://forum.ionicframework.com/t/ionic-modal-service-with-extras/15357

+0

おかげアーディティヤ、私はそれをやってみますよ。本当に助けに感謝します。 –

+0

私はあなたが探していた正確な実装を持っているionic forumへのリンクを追加しました –

関連する問題