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

aft*_*eep 12 javascript twitter-bootstrap angularjs bootstrap-modal ui.bootstrap

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

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

var modalInstance = $uibModal.open({
      templateUrl: 'addEditModal.html',
      scope: $scope
    });
Run Code Online (Sandbox Code Playgroud)

但是,由于我没有注入范围,并且使用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;
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

有更好的想法吗?

Est*_*ask 15

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

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

var modalInstance = $uibModal.open({
  templateUrl: 'addEditModal.html',
  scope: $scope
});
Run Code Online (Sandbox Code Playgroud)

不注入范围但指定模态控制器的父范围(否则根范围将用作父范围).

由于controllerAs用于父控制器,因此模态控制器可以访问vm其作用域上的继承对象.


aft*_*eep 8

不确定如果我理解正确,但我通过在resolve参数中传递/注入当前'controllerAs'来实现它

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

然后,在AudioItemAddEditCtrl中......

var AudioItemAddEditCtrl = function(parent, AudioItemService, $ModalInstance) {
...
}
Run Code Online (Sandbox Code Playgroud)

然后我就可以使用'parent'直接访问父控制器作用域.

希望这有助于其他人.