Yalamber
Yalamber

Reputation: 7580

Unable to access $rootScope in factory

I am having a strange error with angular.js I am using version 1.2.26. Below is my code, I am not able to access properties I set in $rootScope. When I log $rootScope I can see those properties. But I am not able to access the properties.

angular
  .module('ids.factories', [])
  .factory('StoreFactory', ['$rootScope', function ($rootScope) {
    console.log($rootScope);//This is defined and i can see the store property
    console.log($rootScope.store);//This seems to be undefined
    return {
      get: function (key, callback) {
        return $rootScope.store.get(key, callback)
      },
      save: function (key, data, callback) {
        $rootScope.store.save({
          key: key,
          value: data
        }, callback);
      },
      remove: function (key) {
        $rootScope.store.remove({key: key});
      }
    }
  }]);

enter image description here

Upvotes: 0

Views: 722

Answers (1)

alxU
alxU

Reputation: 93

This error depends on how the developer tools of your Browser are working. Like tasseKATT said in his сomment, the console.log() got called before the Variable is initialized. Later in the Console, you see only a reference with the evaluated code. The Developer - Tool gives you a hint by a small blue Sign besides the console.log entry. (you see it in your screenshot, too) Greetings!

Upvotes: 1

Related Questions