Javascript 使用 controllerAs 语法时将当前范围传递给 modalInstance

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33164281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:43:18  来源:igfitidea点击:

Pass current scope to modalInstance when using controllerAs syntax

javascriptangularjstwitter-bootstrapbootstrap-modalui.bootstrap

提问by afterxleep

I'm using controllerAs syntax to avoid a $scope soup in my controllers, and also using ui.bootstrap to present a modal view.

我使用 controllerAs 语法来避免控制器中的 $scope 汤,并使用 ui.bootstrap 来呈现模式视图。

I need to open a modalInstace that shares the same scope as the current controller. When injecting the scope, you could probably do something like:

我需要打开一个与当前控制器共享相同范围的 modalInstace。注入范围时,您可能会执行以下操作:

var modalInstance = $uibModal.open({
      templateUrl: 'addEditModal.html',
      scope: $scope
    });

However as I'm not injecting the scope, and using controllerAs syntax, that will not work.

但是,由于我没有注入范围,并且使用 controllerAs 语法,这将不起作用。

From what I've found, you will need to use resolve to pass the data, but you have to pass it explicitly via functions. Is there a way to pass the entire scope?

根据我的发现,您将需要使用 resolve 来传递数据,但您必须通过函数显式传递它。有没有办法传递整个范围?

There is a bunch of stuff I need to do in that modal and passing loads of data seems overkill.

在该模式中我需要做很多事情,传递大量数据似乎有点过头了。

Don't want to do this, as it seems messy...

不想这样做,因为它看起来很乱......

var modalInstance = $modal.open({
  templateUrl: 'myModalContent.html',
  controller: 'ModalInstanceCtrl',
  resolve: {
    user: function() {
        return vm.user;
    },
    something: function() {
        return vm.something;
    },
    blah: function() {
        return blah;
    }
  }
});

Any better ideas?

有什么更好的想法吗?

回答by Estus Flask

I need to open a modalInstace that shares the same scope as the current controller.

我需要打开一个与当前控制器共享相同范围的 modalInstace。

Modal service creates inherited scope. And

模态服务创建继承范围。和

var modalInstance = $uibModal.open({
  templateUrl: 'addEditModal.html',
  scope: $scope
});

does not inject the scope but specifies parent scope for modal controller (otherwise root scope will be used as the parent).

不注入作用域,而是为模态控制器指定父作用域(否则根作用域将用作父作用域)。

Since controllerAs was used on parent controller, modal controller will have access to inherited vmobject on its scope.

由于在父控制器上使用了 controllerAs,模态控制器将可以访问vm其范围内的继承对象。

回答by afterxleep

Not sure If I understood correctly, but I got it working by passing/injecting the current 'controllerAs' in the resolve parameter

不确定我是否理解正确,但我通过在解析参数中传递/注入当前的“controllerAs”来使其工作

var modalInstance = $uibModal.open({
      templateUrl: 'addEditModal.html',
      controller: 'AudioItemAddEditCtrl as vm',
      resolve: {
        parent: function(){
            return vm
        }
    }
    });

And then, in the AudioItemAddEditCtrl...

然后,在 AudioItemAddEditCtrl...

var AudioItemAddEditCtrl = function(parent, AudioItemService, $ModalInstance) {
...
}

Then I'm able to use 'parent' to access the parent controller scope directly.

然后我可以使用“父”直接访问父控制器范围。

Hope this helps someone else.

希望这对其他人有帮助。