AngularJS/Typescript - 在未注册的基础服务中访问 $http
AngularJS/Typescript - Access $http in unregisterd base service
我有多项服务,它们都扩展了我的 BaseService
class,如下所示:
module app.services {
export class PostService extends BaseService {
constructor() {
super('post');
}
}
export var app:ng.IModule = app || angular.module('app.services.post');
app.service('PostService', PostService);
}
在我的 BaseService
中,我将使用 angularjs 提供的 $http
工具提出请求。 BaseService
不是注册的 angularJs 模块(它只是我的注册服务扩展的打字稿 class):
module app.services {
export class BaseService {
protected $http: any;
baseRoute:string;
constructor(baseRoute:string) {
if (baseRoute.indexOf('api/') === 0) {
this.baseRoute = baseRoute;
} else {
this.baseRoute = 'api/' + baseRoute;
}
}
get(id: number) {
this.$http.get...
}
}
所以 PostService
扩展了 BaseService
。在这种情况下,我如何才能访问 $http
?理想情况下,我的 BaseController
将处理获取 $http
,因此它的所有子服务都不必发送 $http
。我只想处理设置 $http
并在 BaseController
中使用它。我觉得我应该能够以某种方式注入它:
static $inject = ['$http'];
但我没有任何运气。
使用bootstrap
函数获取$injector并将其放在windowhttps://docs.angularjs.org/guide/bootstrap上。
然后在代码中的任意位置使用 $injector.get('$http')。
我有多项服务,它们都扩展了我的 BaseService
class,如下所示:
module app.services {
export class PostService extends BaseService {
constructor() {
super('post');
}
}
export var app:ng.IModule = app || angular.module('app.services.post');
app.service('PostService', PostService);
}
在我的 BaseService
中,我将使用 angularjs 提供的 $http
工具提出请求。 BaseService
不是注册的 angularJs 模块(它只是我的注册服务扩展的打字稿 class):
module app.services {
export class BaseService {
protected $http: any;
baseRoute:string;
constructor(baseRoute:string) {
if (baseRoute.indexOf('api/') === 0) {
this.baseRoute = baseRoute;
} else {
this.baseRoute = 'api/' + baseRoute;
}
}
get(id: number) {
this.$http.get...
}
}
所以 PostService
扩展了 BaseService
。在这种情况下,我如何才能访问 $http
?理想情况下,我的 BaseController
将处理获取 $http
,因此它的所有子服务都不必发送 $http
。我只想处理设置 $http
并在 BaseController
中使用它。我觉得我应该能够以某种方式注入它:
static $inject = ['$http'];
但我没有任何运气。
使用bootstrap
函数获取$injector并将其放在windowhttps://docs.angularjs.org/guide/bootstrap上。
然后在代码中的任意位置使用 $injector.get('$http')。