2015-07-01 59 views
0

嗨,我是新來的angularjs和任何幫助將不勝感激。我授權用戶提供服務,我的服務看起來像這樣嘗試從另一個控制器訪問變量時獲取一個未定義的變量angularjs

'use strict'; 
app.factory('Auth', function ($firebaseSimpleLogin, FIREBASE_URL, $rootScope, $firebase) { 
    var ref = new Firebase(FIREBASE_URL); 
    var auth = $firebaseSimpleLogin(ref); 
    var Auth = { 
     register: function (user) { 
      return auth.$createUser(user.email, user.password); 
     }, 
     createProfile: function (user) { 
      var profile = { 
       userfirstname: user.fname, 
       userlastname: user.lname, 
       username: user.fname + " " + user.lname, 
       userprofiletags: user.profiletags 
      }; 
      var profileRef = $firebase(ref.child('profile')); 
      return profileRef.$set(user.uid, profile); 
     }, 
     login: function (user) { 
      return auth.$login('password', user); 
     }, 
     logout: function() { 
      auth.$logout(); 
     }, 
     resolveUser: function() { 
      return auth.$getCurrentUser(); 
     }, 
     signedIn: function() { 
      return !!Auth.user.provider; 
     }, 
     user: {} 
    }; 
    $rootScope.$on('$firebaseSimpleLogin:login', function (e, user) { 
     console.log('logged in'); 
     angular.copy(user, Auth.user); 
     Auth.user.profile = $firebase(ref.child('profile').child(Auth.user.uid)).$asObject(); 
     console.log(Auth.user); 
    }); 
    $rootScope.$on('$firebaseSimpleLogin:logout', function() { 
     console.log('logged out'); 
     if (Auth.user && Auth.user.profile) { 
      Auth.user.profile.$destroy(); 
     } 
     angular.copy({}, Auth.user); 
    }); 
    return Auth; 
}); 

我想從控制器訪問userprofiletags變量編碼下面

'use strict'; 
app.controller('PostsCtrl', function ($scope, $http, Post, $location, Auth) { 
    $scope.user = Auth.user; 
    $scope.signedIn = Auth.signedIn; 
    $scope.logout = Auth.logout; 
    $scope.usertags = $scope.user.profile.userprofiletags; 
    console.log($scope.usertags); 
    $scope.loadTags = function (query) { 
     return $http.get('support/tags.json'); 
    }; 
    $scope.onChange = function (cbState) { 
     $scope.message = "The switch is now: " + cbState; 
    }; 
    $scope.posts = Post.all; 
    $scope.post = { 
     title: '', 
     description: '', 
     tags: [], 
     Anonymous: 'false' 
    }; 
    var createdtime = moment().format('MMMM Do YY, h:mm:ss a'); 
    $scope.submitQuestion = function() { 
     $scope.post.createdTime = createdtime; 
     $scope.post.creator = $scope.user.profile.username; 
     $scope.post.creatorUID = $scope.user.uid; 
     Post.create($scope.post).then(function (ref) { 
      $location.path('/posts/' + ref.name()); 
     }); 
    }; 
    $scope.deletePost = function (post) { 
     Post.delete(post); 
    }; 
}); 

當我試圖從訪問變量使用{{user.profile.userprofiletags}}查看它給我的價值觀。但是我試圖在我的PostsCtrl中將變量賦值給$ scope.usertags時,結果變得不確定。

如果我不夠清楚,我會道歉。請幫助我解決這個錯誤。非常感謝您的幫助。

回答

0

由於Firebase調用是異步的,因此在此時未定義Auth.user。你可以做這樣的事情在你的控制器:

Auth.resolveUser().then(function(user) { 
    $scope.usertags = user.profile.userprofiletags; 
}); 
+0

真的對於錯字抱歉..我更新了代碼,它仍然有相同的錯誤。 –

1

我覺得這裏的問題是執行console.log撥打電話所以基本上$scope.user.profile.userprofiletags不會在這個時間點之後存在很短,實際上它此事件被廣播'$firebaseSimpleLogin:login'

將準備你可以使用$手錶,並摧毀它接收數據

watchDestroyer = $scope.$watch('user.profile', function (newUserProfile) { 
    if (typeof newUserProfile !== "undefined" && newUserProfile !== null) { 
     $scope.user = newUserProfile 
     watchDestroyer() 
    } 
}) 

後或利用事件或使用承諾

相關問題