1

I need to write an angular module that uses $http

(function () {
    'use strict';

    var app = angular.module('myModule', []);

    app.provider("$late", function () {
        var file;
        var remoteFile
        var data = {};
        var separator;

        return ({
            $get: instantiateLate,
            Load: Load
        });
        function Load(config) {
            console.log("load");
            this.file = (config.file !== undefined) ? config.file : "";
            this.remoteFile = (config.remoteFile !== undefined) ? config.remoteFile : "";
            this.separator = (config.separator !== undefined) ? config.separator : ".";

            if (this.remoteFile === undefined && this.remoteFile === undefined)
                throw new Error('Specificare un file locale o remoto');

            //$http.get ....
        }
        function instantiateLate() {
            return ({
                file: file,
                remoteFile: remoteFile,
                data: data,
                separator: separator
            });
        }
    });

}());

How can I use the $http? When I uncomment $http.get I get this error:

ReferenceError: $http is not defined

thanks

1
  • what are the angular files you are using? and what does your module registration look like? Commented Mar 31, 2016 at 16:12

4 Answers 4

2

To fix your error, you need to inject $http.

app.provider("$late", ['$http', function ($http) { ... }]

Edit: However, since you are using a provider, you cannot inject a service into it. You can inject it in your $get though. Instead of the above for a provider, use the following.

    function instantiateLate($http) {
        return ({
            file: file,
            remoteFile: remoteFile,
            data: data,
            separator: separator
        });

Source: use $http inside custom provider in app config, angular.js

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

3 Comments

when I do this, this is the new error: "unknown provider: $http"
I updated my post. It turns out you cannot inject a service directly into a provider.
your link source has given me the correct way, thanks
0

For providers, runtime service instances are injected in the $get constructor function.

$get: ["$http", instantiateLate($http)],

This means your code needs to be re-factored to work.

   function instantiateLate($http) {
       //
       //Put functions that uses $http here
       // 
       return ({
            file: file,
            remoteFile: remoteFile,
            data: data,
            separator: separator
       });
    }

Comments

0

Your approach is wrong. The providers don't uses return statement for create the instance. check the correct way to create a provider in this docs and this article.

My recommendation it's do the follow:

(function () {
    'use strict';

    var app = angular.module('myModule', []);

    app.provider("$late", function () {
        var file;
        var remoteFile;
        var data = {};
        var separator;

        var httpReference;

        this.load = Load;
        this.$get = instantiateLate;

        function Load(config) {
            console.log("load");
            this.file = (config.file !== undefined) ? config.file : "";
            this.remoteFile = (config.remoteFile !== undefined) ? config.remoteFile : "";
            this.separator = (config.separator !== undefined) ? config.separator : ".";

            if (this.remoteFile === undefined && this.remoteFile === undefined)
                throw new Error('Specificare un file locale o remoto');

            httpReference.get('some_url');
        }

        instantiateLate.$inject = ['$http'];

        function instantiateLate($http) {
             httpReference = $http;
             return ({
                file: file,
                remoteFile: remoteFile,
                data: data,
                separator: separator
            });
        }
    });

}())

Comments

0

with this code it works

var initInjector = angular.injector(['ng']);
var $http = initInjector.get('$http');

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.