我正在爲Firebase電子郵件&密碼登錄製作UI Bootstrap模式窗口。模態窗口登錄用戶並返回authData
對象。模態窗口然後自行關閉。然後authData
對象不可用於主窗口或家庭控制器。
看來,模式窗口或其控制器正在製作一個孩子$scope
。 authData
對象可用於子$範圍,但不在父$scope
上。
這裏是home.html的按鈕執行的代碼,打開模態窗口:
<button type="button" class="btn btn-info" ng-click="openModal('md')">
E-mail & 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中獲取代碼。
有什麼建議嗎?
^這是方式。 您也可以傳入模態實例的回調函數來訪問$ uibModal中的變量作用域...這很好,通過添加解析。 var callingFn = function(){// do stuff}; VAR modalInstance = $ uibModal.open({ 大小:大小, templateUrl://..etc 決心:{passFn:callingFn} – razblack
正確答案:-)現在我明白了'''接近(結果) '''在文檔中。謝謝! –