angularjs自定义模块与$http注入
angularjs custom module with $http injection
我需要编写一个使用$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');
相关文章:
- 使用Objective-C的JavaScript注入方法
- Meteor如何接收HTTP请求
- 正在注入包含JS的HTML
- 迷你$provider注入茉莉花和角
- Angularjs工厂注入错误
- angularjs自定义模块与$http注入
- javascript注入$http-post方法不起作用
- 如何在没有打字稿的情况下在 Angular 2 中注入 Http
- 尽管注入了$http,但它在服务中是未定义的
- Ionic 2-导入/注入Angular 2 HTTP组件
- 注入$http在使用typescript的jasmine测试中失败
- 错误:将Angular$http服务注入需要插件
- 即使在注入http之后,Uncaught TypeError: Cannot read property 'ge
- 注入和使用Http
- 无法使用AngularJS显式注入' $http '.控制器的语法
- 在$http拦截器中注入$http服务
- 使用$http注入的自定义指令
- 如何在一条注入语句中注入$scope、$http、$q、$location这四个变量
- 如何将自定义HTTP标头注入SuperAgent发出的每个请求中
- 将$http注入angular“;使用严格的“;服务