使用 controllerAs 语法时将当前范围传递给 modalInstance [英] Pass current scope to modalInstance when using controllerAs syntax

查看:23
本文介绍了使用 controllerAs 语法时将当前范围传递给 modalInstance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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:

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

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

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

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

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?

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

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;
    }
  }
});

有更好的想法吗?

推荐答案

我需要打开一个与电流控制器.

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

模态服务创建继承范围.还有

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

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

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

这篇关于使用 controllerAs 语法时将当前范围传递给 modalInstance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆