angularfire - Getting an undefined variable while trying to access a variable from another controller angularjs -


hi new angularjs , appreciated. authorising users service , service looks

'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; }); 

i trying access userprofiletags variable controller coded below

'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 now: " + cbstate;     };     $scope.posts = post.all;     $scope.post = {         title: '',         description: '',         tags: [],         anonymous: 'false'     };     var createdtime = moment().format('mmmm 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);     }; }); 

when try access variable view using {{user.profile.userprofiletags}} gives me values. getting undefined result when try assign variable $scope.usertags in postsctrl.

i apologise if not clear enough. please me error. appreciated.

i think problem here console.log short after making call $scope.user.profile.userprofiletags doesn't exist @ point in time, in fact ready when event broadcasted '$firebasesimplelogin:login'

you can either use $watch , destroy after receiving data

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

or utilise event or use promises


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -