Angularjs 在Ionic中,我应该将模式代码移到哪里?

Angularjs 在Ionic中,我应该将模式代码移到哪里?,angularjs,ionic-framework,Angularjs,Ionic Framework,在我的控制器中,启动了两个模态。其中一个的代码是: function setUpEditCommentModal() { $ionicModal.fromTemplateUrl('app/recent/edit-comment-modal.html', { scope: $scope, // so close methods available. animation: 'fade-in-scale' }).th

在我的控制器中,启动了两个模态。其中一个的代码是:

   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
        });
    }
另一个模态代码甚至更长。 重构这段代码以使我的控制器类不太大的最佳方法是什么


我应该将此代码移动到服务中吗?

您可以创建一个
ModalService
,它将有一个
register()
方法。这可以一次注册多个模态并返回一个承诺。承诺的ResResResolved值可以本地保存,也可以通过引用从服务中使用。为了通过引用使用它,我们需要存储服务中所有当前活动模态的列表。该服务将如下所示:

.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
    });
  }
});
此JSBin中提供了使用上述工厂的控制器中2个模态的更简单实现:。此外,您还可以在服务的本地阵列中实现活动模态列表,并根据新的/已删除的模态添加或删除


更新:可重用方法显示模态的服务:

谢谢Aditya,我会尝试一下。真的很感谢你的帮助。我已经添加了到爱奥尼亚论坛的链接,它有你想要的确切实现