使用controllerAs语法时,通过电流范围modalInstance [英] Pass current scope to modalInstance when using controllerAs syntax

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

问题描述

我使用controllerAs语法来避免在控制器上$范围汤,还用ui.bootstrap至present模态的视图。

我需要打开一个共享的同一范围内的电流控制器modalInstace。当注射的范围,你很可能做这样的事情:

VAR modalInstance = $ uibModal.open({
      templateUrl:addEditModal.html',
      适用范围:$范围
    });

不过因为我不是注射的范围,并使用controllerAs语法,这是行不通的。

从什么我发现,你将需要使用的决心来传递数据,但是你必须通过功能明确传递。有没有一种方法来传递整个范围是什么?

有东西一大堆,我需要在这模式做,数据传递的负荷似乎矫枉过正。

不想这样做,因为它似乎凌乱...

VAR modalInstance = $ modal.open({
  templateUrl:myModalContent.html',
  控制器:'ModalInstanceCtrl',
  解析:{
    用户:函数(){
        返回vm.user;
    },
    东西:函数(){
        返回vm.something;
    },
    胡说:功能(){
        返回等等;
    }
  }
});

任何更好的想法?


解决方案

  

我需要打开一个共享的同一范围内一个modalInstace
  电流控制器。


莫代尔服务会创建继承范围。以及

  VAR modalInstance = $ uibModal.open({
  templateUrl:addEditModal.html',
  适用范围:$范围
});

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

由于controllerAs被父控制器上使用,模态控制器将对其范围获得继承 VM 对象。

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

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

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

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

Any better ideas?

解决方案

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

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 vm object on its scope.

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

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