2016-04-26 75 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

正确答案:-)现在我明白了'''接近(结果) '''在文档中。谢谢! –