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

Mauro Sala
  • 1,166
  • 2
  • 12
  • 33
  • what are the angular files you are using? and what does your module registration look like? – Nilesh Mar 31 '16 at 16:12

4 Answers4

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

Community
  • 1
  • 1
rabruce
  • 133
  • 1
  • 8
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
       });
    }
georgeawg
  • 48,608
  • 13
  • 72
  • 95
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
            });
        }
    });

}())
0

with this code it works

var initInjector = angular.injector(['ng']);
var $http = initInjector.get('$http');
Mauro Sala
  • 1,166
  • 2
  • 12
  • 33