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

Pass current scope to modalInstance when using controllerAs syntax

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

我需要打开一个与当前控制器共享相同范围的 modalInstace。注入范围时,您可能会做类似的事情:

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

然而,由于我没有注入作用域,而是使用 controllerAs 语法,所以这将不起作用。

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

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

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

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.

模态服务creates inherited scope。并且

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

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

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

不确定我是否理解正确,但我通过 passing/injecting 解析参数

中的当前 'controllerAs' 使其正常工作
var modalInstance = $uibModal.open({
      templateUrl: 'addEditModal.html',
      controller: 'AudioItemAddEditCtrl as vm',
      resolve: {
        parent: function(){
            return vm
        }
    }
    });

然后,在 AudioItemAddEditCtrl 中...

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

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

希望这对其他人有帮助。