1

UPDATE 2:

I found out where the problem was coming from and I left out the important part.

I'm setting the property in the first controller INSIDE this

$rootScope.$on('anEvent', function (event, data) {
    InjectedService.setToken(data.value);
})

But I can't grab it from outside that callback scope. The event that is listened for depends on an $http request so I'm guessing that is the problem. Are there any workarounds to this?

UPDATE:

The order of controllers are the other way around so that the code in secondController is actually being called first in my actual app. I have reordered them accordingly

Question:

I'm trying to grab the services specific property but when I try to grab the service property, I get undefined (using a getter function or not). But when I try to grab the full service, I get everything with the correct property and value.

main.js

angular.module('myApp', ['myModule', 'myServices'])
    .controller('firstController', ['InjectedService', function(InjectedService) {

            InjectedService.setProperty('Hello World')

    }])

othermodule.js:

angular.module('myModule', [])
    .controller('secondController', ['InjectedService',
        function (InjectedService) {

            console.log(InjectedService); // Full object with property == 'hello world'
            console.log(InjectedService.property); // I get undefined
            console.log(InjectedService.getProperty()); // I get undefined

            // interesting to note:
            InjectedService.setToken('A different string');
            console.log(InjectedService.property); // I get 'A different string'
            console.log(InjectedService); // Full object with property == 'Hello World' which was set in second controller

        }])

services.js:

angular.module('myServices', function
    .service('InjectedService', function(){
        var Service = this;

        Service.setProperty = function (value) {
            Service.property = value;
        }

        Service.getProperty = function () {
            return Service.property;
        }

        Service.unsetProperty = function () {
            Service.property = null;
        }

        return Service;
    })

It seems to me a scope problem, but the variable isn't a primitive type. Any suggestions?

Jeff
  • 1,928
  • 1
  • 12
  • 18
  • 1
    secondController isn't being called before firstController – JoseM Oct 30 '14 at 04:59
  • Sorry, I should probably reorder that, in my actual app its the other way around. – Jeff Oct 30 '14 at 05:08
  • It doesn't matter which way it appears in the source code, what matters is the order in which the controllers are being called. – JoseM Oct 30 '14 at 05:10
  • I've tried swapping where the token was set and where the token was grabbed. I'm still getting undefined. EDIT: Token == property i just swapped the variable name for the question EDIT 2: I'm sorry I left an important part of the code out. I found out what the problem is but that leads to another problem so let me edit the question. – Jeff Oct 30 '14 at 05:15
  • 1
    See this question and the answers: http://stackoverflow.com/questions/12576798/how-to-watch-service-variables -- you are doing the same thing – JoseM Oct 30 '14 at 05:37

0 Answers0