$范围在ng-click模式下打开模式时丢失

$范围在ng-click模式下打开模式时丢失

问题描述:

当我试图在ng-click上打开模式时,scipe变量正在丢失。

var modalInstance = $modal.open({ 
templateUrl: 'partials/create.html', 
controller: 'AppCreateCtrl', 
scope: $scope // <-- I added this 
}); 

你应该使用resolve属性这样传递数据:

var modalInstance = $modal.open({ 
      templateUrl: templateSrv.templateUrl('partials/create.html'), 
      controller: modalBootstrap, 
      backdrop: 'static', 
      keyboard: false, 
      resolve: { 
       data: function() { 
        var result = {}; 
        result.scope = $scope; 
        return result; 
       } 
      } 
     }); 

modalInstance.result.then(
      function (dataPassedFromModalConroller) { 
       //this is called when the modal is closed 
      }, 
      function (dataPassedFromModalConroller) { //Dismiss 
       //this is called when the modal is dismissed 
      } 
     ); 

指定这样的模式实例的控制器(我们这样做是在另一个文件中):

var modalBootstrap= function ($scope, $modalInstance, data) { 
    $scope.modalInstance = $modalInstance; 
    $scope.scope = data.userId; 
}; 
modalBootstrap['$inject'] = ['$scope', '$modalInstance', 'data']; 

你的模态模板看起来像这样:

<div ng-controller="AppCreateCtrl">modal content here</div> 
+0

好了之后,我将访问范围变量,如result.myVariable –

+0

不,在您的模态控制器中,您可以像访问$ scope.scope一样访问它。被注入的数据被放在你的模态$范围 – nweg

+0

$ scope.scope.myVariable像这样 –