As it is promise, you need to move code to route.resolve. It will wait for
promise to resolve. 'run' will not wait, I think, that's why you are
getting null.
Best luck.
On Saturday, June 21, 2014 4:28:40 PM UTC-5, Jason Girdner wrote:
>
> Ok I am beating my head into the wall trying to figure out why my service
> is returning a undefined value on page load. Below is my service and the
> run function I have. I can see from my console log that I am getting the
> correct value. But when I log ($rootScope.user) from my app controller I
> get undefined. I can see in the Angular inspector that the data I am
> trying to get in the controller is in the rootScope but for some reason its
> loading undefined before it has a chance to fetch the data. I have played
> around for hours with the promise function and can't seem to get it to
> work. Any help will be appreciated.
>
> .service('CurrentUser', function($http, $q) {
> this.get = function() {
> var deferred = $q.defer();
> var url = '/api/current-user?format=json';
> $http.get(url).success(function(data, status) {
> deferred.resolve(data);
> }).error(function(data, status) {
> deferred.reject(data);
> });
> return deferred.promise;
> }
> })
> .run(function (CurrentUser, $rootScope){
> var promise = CurrentUser.get();
> promise.then(function(data) {
> if (data.is_active == false) {
> $rootScope.authenticated = false
> $rootScope.user = null
> console.log($rootScope.authenticated)
> } else {
> $rootScope.authenticated = true
> $rootScope.user = data;
> console.log($rootScope.authenticated)
> }
> });
> })
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.