Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I have a angular app with following structure

masterApp.js :

var masterApp = angular.module('masterApp', []);
masterApp.factory("organizationFactory", ["$http", organizationFactory(http)]);
masterApp.controller("organizationCtrl", ["$scope", "organizationFactory",organizationCtrl(scope,organizationFactory)]);

organizationFactory.js:

function organizationFactory(http) {
    return {

        getDefaultOrganization:function() {
            http.get(organizationRestUrl);
        },
        registerOrganization:function(organization) {
            http.post(organizationRestUrl, organization);
        },
        updateOrganization:function(organization) {
            http.put(organizationRestUrl, organization);
        },
        deleteOrganization:function() {
            http.delete(organizationRestUrl);
        }
    };
}

OrganizationCtrl.Js

function organizationCtrl($scope,organizationFactory) {

    alert(scope);
    scope.organization = organizationFactory.getDefaultOrganization();
}

When app runs i got an error

ReferenceError: http is not defined

masterApp.factory("organizationFactory", ["$http", organizationFactory(http)])

I changed to injecting http to $http and scope to $scope ( i think it will not cause the error because i am using appropriate minfication safe guidelines) .but still i am stuck with that error NB: I am sure that angular.js loading before the script

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You should pass a reference to the function instead of invoking the function and getting it returned value.

With this code:

organizationFactory(http)

You're invoking the function passing in http which does not exist in that context.

To pass a function reference, just organizationFactory is enough.

Replace:

masterApp.factory("organizationFactory", ["$http", organizationFactory(http)]);
masterApp.controller("organizationCtrl", ["$scope", "organizationFactory",organizationCtrl(scope,organizationFactory)])

With:

masterApp.factory("organizationFactory", ["$http", organizationFactory]);
masterApp.controller("organizationCtrl", ["$scope", "organizationFactory",organizationCtrl])
share|improve this answer
    
yes u r right @khanh. i forgot the basics of javascript. thanks – Binson Eldhose May 11 '14 at 4:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.