I have written an angular provider that needs $injector service to be injected in the $get function but I do not know how to write this in typescript and make it minification safe. Something like static $injector = ['']; notation that works for services and controllers.
Typescript:
export class ApiProvider implements IApiProvider {
private baseRoute: string = '';
private endpoints: { [name: string]: IApiEndPointConfig };
static $inject = ['$injector']; //THIS DOES NOT WORK FOR PROVIDERS
constructor() {
this.baseRoute = '';
this.endpoints = {};
}
// MORE CODE
$get = ($injector: ng.auto.IInjectorService): { [name: string]: ApiEndpoint } => {
var api: { [name: string]: ApiEndpoint } = {};
var self:ApiProvider = this;
angular.forEach(this.endpoints, (endpointConfig, name) => {
api[name] = $injector.instantiate(ApiEndpoint, {
baseRoute: self.baseRoute,
endpointConfig: endpointConfig
});
});
return api;
};
}
The generated javascript for the $get function is:
this.$get = function ($injector) {
var api = {};
var self = _this;
angular.forEach(_this.endpoints, function (endpointConfig, name) {
api[name] = $injector.instantiate(Model.ApiEndpoint, {
baseRoute: self.baseRoute,
endpointConfig: endpointConfig
});
});
return api;
};
And what I want it to be is something like:
this.$get = ['$injector', function ($injector) {
var api = {};
var self = _this;
angular.forEach(_this.endpoints, function (endpointConfig, name) {
api[name] = $injector.instantiate(Model.ApiEndpoint, {
baseRoute: self.baseRoute,
endpointConfig: endpointConfig
});
});
return api;
}];
I think I got it. After reading the $injector documentation more closely I found that the $inject string array notation can be used on any function. Thus I did something like:
constructor(){
this.$get.$inject = ['$injector'];
}
$get = ($injector: ng.auto.IInjectorService): { [name: string]: ApiEndpoint } => {
//using $injector
};
which produces:
this.$get = function ($injector) {
//using $injector
};
this.$get.$inject = ['$injector'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With