2016-04-26 46 views
0

我正在爲Firebase電子郵件&密碼登錄製作UI Bootstrap模式窗口。模態窗口登錄用戶並返回authData對象。模態窗口然後自行關閉。然後authData對象不可用於主窗口或家庭控制器。

看來,模式窗口或其控制器正在製作一個孩子$scopeauthData對象可用於子$範圍,但不在父$scope上。

這裏是home.html的按鈕執行的代碼,打開模態窗口:

<button type="button" class="btn btn-info" ng-click="openModal('md')"> 
    E-mail &amp; password</button> 

這裏是HomeController.js打開的模式窗口代碼:

$scope.openModal = function(size) { 
    var modalInstance = $uibModal.open({ 
     templateUrl: 'javascript/templates/emailLoginModalContent.html', 
     controller: 'EmailLoginModalInstanceCtrl', 
     scope: $scope, 
     size: size 
    }); 
    }; 

請注意,我設置了scope: $scope。我也試過scope: $parent

這裏是模態窗口的控制器的一部分:

app.controller('EmailLoginModalInstanceCtrl', ['$scope', '$uibModalInstance', '$firebaseAuth', function($scope, $uibModalInstance, $firebaseAuth) { 
    console.log("EmailLoginModalInstanceCtrl controller."); 

    var ref = new Firebase("https://my-firebase.firebaseio.com/"); 
    $scope.authObj = $firebaseAuth(ref); 

    // Login user 

    $scope.loginUser = function(user) { 
     ref.authWithPassword({ 
     email: $scope.user.email, 
     password: $scope.user.password 
     }, function(error, authData) { 
     if (error) { 
      console.log("Login Failed!", error); 
     } else { 
      console.log("Authenticated successfully with payload:", authData); 
      $scope.authData = authData; 
      $scope.authData.uid = authData.uid; 
      console.log($scope.authData.uid); 
      $scope.reset(); 
      $scope.cancel(); 
      $scope.$apply(function() { 
      console.log("Applied!"); 
      }); 
     } 
     }); 
    }; 

    }]); 

請注意,我試圖$scope.authData = authData;$scope.authData.uid = authData.uid;。都不能將authData對象放在父$範圍內。

我也嘗試從家庭控制器運行$getAuth()。得到了authData對象並將其放在父項$scope上。但是當模式窗口關閉時,我無法在Home Controller中獲取代碼。

有什麼建議嗎?

回答

2

您可以從模態控制器,以這樣的家長控制功能,通過該值,

$scope.ok = function(){ 
    $uibModalInstance.close({ authData : authData}) 
    } 

注意,模態的還好按鈕必須調用模態控制器上ng-click$scope.ok

現在,在您的父控制器中,$ uibModal.open返回一個包含result屬性的對象。

var modalInstance = $uibModal.open({ 
     templateUrl: 'javascript/templates/emailLoginModalContent.html', 
     controller: 'EmailLoginModalInstanceCtrl', 
     scope: $scope, 
     size: size 
    }) 
modalInstance.result.then(function(authData){ 
    console.log('printing authData - ', authData) 
}) 
+0

^這是方式。 您也可以傳入模態實例的回調函數來訪問$ uibModal中的變量作用域...這很好,通過添加解析。 var callingFn = function(){// do stuff}; VAR modalInstance = $ uibModal.open({ 大小:大小, templateUrl://..etc 決心:{passFn:callingFn} – razblack

+0

正確答案:-)現在我明白了'''接近(結果) '''在文檔中。謝謝! –