angularjs自定义模块与$http注入

angularjs custom module with $http injection

本文关键字:http 注入 自定义 模块 angularjs      更新时间:2023-10-21

我需要编写一个使用$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
            });
        }
    });
}());

如何使用$http?当我取消注释$http.get时,我会得到以下错误:

ReferenceError:$http未定义

感谢

要修复错误,需要注入$http。

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

编辑:但是,由于您使用的是提供程序,因此无法向其中注入服务。不过,您可以将其注入$get中对于提供程序,请使用以下内容,而不是以上内容

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

来源:在应用配置中的自定义提供程序中使用$http,angular.js

对于提供程序,运行时服务实例被注入$get构造函数。

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

这意味着您的代码需要重新考虑才能工作。

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

您的方法是错误的。提供程序不使用return语句来创建实例。在本文档和本文中检查创建提供程序的正确方法。

我的建议是这样做:

(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
            });
        }
    });
}())

使用此代码,即可工作

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