1

I'm trying to create a universally accessible array in my application using services. I also implement ui-router in my appliaction. I create the service in my app.js:

myFamilyApp.service('global', function() {
    var eventArray = [];

    return {
        addEvent: function() {
            eventArray.push(newObj);
        }
    }
});

then I call it from my controller:

myFamilyApp.controller('newEventController', [ '$scope', '$state',
    function($scope, $state, global) {
    eventConfig = {//variables to create event};
    e = new Event(eventConfig);
    global.addEvent(e);

});

but for some reason I'm getting this error:

TypeError: Cannot read property 'addEvent' of undefined

I know the issue is that the controller not recognizing the global variable, but I don't know why. I've looked at several other posts and generally messed around with it but I can't figure out why my controllers access the service. Any ideas?

1
  • 3
    Typo is in front of your eyes.. :) You forgot to specify it in the injection list. myFamilyApp.controller('newEventController', [ '$scope', '$state', 'global', function($scope, $state, global) { Commented Mar 11, 2015 at 0:28

1 Answer 1

1

I just realized my problem, and I apologize for posting this question I am still very new to angular. My issue was very simple, I wasn't passing in the 'global' when i created my controller myFamilyApp.controller('newEventController', [ '$scope', '$state', 'global', function (params)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.