0

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

1 Answer 1

2

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])
Sign up to request clarification or add additional context in comments.

1 Comment

yes u r right @khanh. i forgot the basics of javascript. thanks

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.