Angularjs 自定义IoniModal指令

Angularjs 自定义IoniModal指令,angularjs,angularjs-directive,ionic-framework,Angularjs,Angularjs Directive,Ionic Framework,我正在尝试创建一个离子模态指令,这样对于应用程序中的每个模态,我就不必加载模板并在控制器中指定关闭和打开功能。我首先创建了以下服务 function ModalService($ionicModal) { this.initialize=function($scope,parent) { parent.modal = Array(); angular.forEach( parent.modals , function(k){ $

我正在尝试创建一个离子模态指令,这样对于应用程序中的每个模态,我就不必加载模板并在控制器中指定关闭和打开功能。我首先创建了以下服务

function ModalService($ionicModal) {

    this.initialize=function($scope,parent) {
        parent.modal = Array();

        angular.forEach( parent.modals , function(k){
            $ionicModal.fromTemplateUrl('templates/'+k+'.html',function(modal)
                {
                    parent.modal[k]=modal;

                },
                {
                    scope:$scope,
                    animation:'slide-in-up'
                });
        });

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

    };

    this.open=function(parent,k) {
        parent.modal[k].show();
    }

    this.close=function(parent,k) {
        parent.modal[k].hide();
    }
}
因此,在控制器中加载我编写的模板

this.modals = ['add-item','edit-item'];

ModalService.initialize($scope,this);
但如果作用域有值,则同一模板可能会填充输入字段。 因此,我创建了指令customModal来指定如下内容

<label custome-modal modal-name="add-item" for="" class="item item-input item-floating-label min-height-65" >
问题是,一旦加载模板,并且模态显示,我就无法访问模态中的控制器。我希望在指令的控制器中指定模态的逻辑,并根据范围值进行操作。怎么做

function modalIngredients($ionicModal) {
    return {
        restrict:'A',
        scope:{
            modalName:'='
        },
        controller:function($scope) {
            $scope.closeCustomModal = function() {
                $scope.modal.hide();
            }
        },
        link:function(scope,elem,attr) {
            $ionicModal.fromTemplateUrl('templates/'+attr.modalName+'.html', {
                scope: scope.externalScope,
                animation: 'slide-in-up'
            }).then(function(modal) {
                scope.modal = modal
            });

            elem.bind('click',function() {
               scope.modal.show();
            });
        }
    }
}