将父控制器传递给模态 (Angular-ui bootstrap)

Passing parent controller to modal (Angular-ui bootstrap)

我觉得这应该可行,但事实并非如此。我希望能够传递打开模式的控制器,以便我可以使用它的提交功能等。我正在使用 controllerAs 模式(因此不使用 $scope)。

var _this = this;

this.openModal = function(modalId,dataIn){
    this.modalInstance = $modal.open({
        templateUrl: modalId,
        controller: 'ModalCtrl',
        controllerAs: 'modal',
        resolve: {
            modalObject: function(){
                return dataIn;
            },
            title: function(){
                return 'Training Info';
            },
            parent: function(){
                return _this
            }
        }
    });

    _this.modalInstance.result.then(function (data) {
        $log.log('submitting data for '+_this.doctorId);
        experienceFactory.submitTraining(data).then(function(){
            loadExperience();
        },function(error){
            console.log(error);
        });
    }, function () {
        //something on close
        $log.info('Modal dismissed at: ' + new Date());
    });
};

this.something = function(){
    $log.warn('omg it workt?');
};

这是用一个简单的 aCtrl.openModal('a-modal',aCtrl.data) 打开的,但是由于某种原因我无法访问 parent 控制器

<script type="text/ng-template" id="a-modal">
    <div class="modal-header">
        <h3 class="modal-title">{{modal.title}}</h3>
    </div>
    <div class="modal-body">
        <button ng-click="parent.something()">Something</button>
    </div>
</script>

该按钮不执行任何操作,但应该打印警告。任何想法表示赞赏。谢谢。

很遗憾,您没有包含控制器的代码,但我认为您误解了如何向模态控制器提供依赖项。

resolve 部分提供了对控制器的依赖,它既不将它们绑定到作用域也不绑定到控制器。看一下 UI-Bootstrap 的模态示例的这个 JS 部分:

.controller('ModalInstanceCtrl', function ($scope, $modalInstance, items) {
    $scope.items = items;

你的情况应该是

.controller('ModalCtrl', ['parent', function (parent) {
    this.parent = parent;

并且在 HTML 中,如果您使用的是 controllerAs 模式:

 <button ng-click="modal.parent.something()">Something</button>