2016-03-08 66 views
0

當我試圖在ng-click上打開模式時,scipe變量正在丟失。

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

回答

2

你應該使用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像這樣 –