简体   繁体   English

将父控制器传递给模态(Angular-ui引导程序)

[英]Passing parent controller to modal (Angular-ui bootstrap)

I feel like this should work, but it's not. 我觉得这应该可行,但事实并非如此。 I want to be able to pass the controller that is opening the modal so I can use its submit function etc. I am using the controllerAs pattern (so not using $scope). 我希望能够传递打开模式的控制器,以便可以使用其提交功能等。我正在使用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?');
};

That is opened up with a simple aCtrl.openModal('a-modal',aCtrl.data) however for some reason I can't access the parent controller 这是通过简单的aCtrl.openModal('a-modal',aCtrl.data)打开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>

The button does nothing, but should be printing the warnings. 该按钮不执行任何操作,但应打印警告。 Any ideas appreciated. 任何想法表示赞赏。 Thanks. 谢谢。

Unfortunately you didn't include the code of your controller, however I think that you misunderstood how dependencies are provided to the modal's controller. 不幸的是,您没有包括控制器的代码,但是我认为您误解了如何将依赖项提供给模态控制器。

The resolve part provides dependencies to the controller, it neither binds them to the scope nor to the controller. resolve部分为控制器提供依赖关系,它既不将它们绑定到作用域,也不绑定到控制器。 Take a look at this JS part of UI-Bootstrap's Modal example: 看一下UI-Bootstrap的Modal示例的JS部分:

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

In your case it should be 在你的情况下应该是

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

And in HTML, if you are using controllerAs pattern: 在HTML中,如果您使用controllerAs模式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM