0
我試圖在用戶的uid
中插入一些東西到我的Firebase數據庫中,但出於某種原因未定義。看看下面的代碼:
,設置該用戶的數據信息(authData
)主控制器在頁面加載時:
flickrApp.controller('mainCtrl', ['$scope', '$rootScope', '$firebase', 'Auth', 'shared', function($scope, $rootScope, $firebase, Auth, shared) {
Auth.$onAuth(function(authData) {
shared.setAuth(authData);
$scope.authData = shared.getAuth();
});
}]);
它處理認證狀態,股吧在我的控制器的服務:
flickrApp.service('shared', function() {
var authentication = false;
return {
getAuth: function() {
return authentication;
},
setAuth: function (auth) {
authentication = auth;
}
};
});
這是它不起作用的地方,在我的標籤控制器中。 $scope.authData
正在$watch
函數中正確設置,但是當我嘗試在var ref
行中使用它時,它說$scope.authData
未定義(因此我無法訪問uid
)。我不知道爲什麼這不是因爲它應該是..
我必須使用$apply
與觀察器功能以及有什麼問題嗎?
flickrApp.controller('tagsCtrl', ['$scope', '$rootScope', '$firebase', 'shared', function($scope, $rootScope, $firebase, shared) {
$scope.tagsList = [];
$scope.shared = shared;
$scope.$watch('shared.getAuth()', function(authData) {
$scope.authData = authData;
console.log($scope.authData);
});
var ref = new Firebase ('https://flickr.firebaseio.com/users/' + $scope.authData.uid);
var sync = $firebase(ref);
$scope.addTag = function(tag) {
$scope.tagsList.push(tag);
sync.$set({favoriteTags: $scope.tagsList});
}
}]);
是的,但我認爲這並不重要,因爲我在設置ref之前運行$ watch。但是,再次,當$ watch首次運行時,認證是錯誤的,所以我想這可以解釋它。無論如何它現在工作。歡呼:) – Chrillewoodz 2015-02-07 09:36:01
如果第一次authData爲false,那麼你應該在條件下執行賦值給ref。我已經更新了答案。 – 2015-02-07 09:39:29
好點,改變。 – Chrillewoodz 2015-02-07 09:50:39