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

时间:2021-09-09 11:55:25

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).

我觉得这应该有效,但事实并非如此。我希望能够传递打开模态的控制器,以便我可以使用其提交功能等。我使用的是控件模式(因此不使用$ 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)打开的,但由于某种原因我无法访问父控制器

<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.

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

1 个解决方案

#1


1  

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.

不幸的是你没有包含你的控制器的代码,但我认为你误解了如何为modal的控制器提供依赖关系。

The resolve part provides dependencies to the controller, it neither binds them to the scope nor to the controller. Take a look at this JS part of UI-Bootstrap's Modal example:

resolve部分为控制器提供依赖关系,它既不将它们绑定到范围也不绑定到控制器。看一下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>

#1


1  

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.

不幸的是你没有包含你的控制器的代码,但我认为你误解了如何为modal的控制器提供依赖关系。

The resolve part provides dependencies to the controller, it neither binds them to the scope nor to the controller. Take a look at this JS part of UI-Bootstrap's Modal example:

resolve部分为控制器提供依赖关系,它既不将它们绑定到范围也不绑定到控制器。看一下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>