The case
I am in the process of upgrading an AngularJS (i.e. Angular 1.6) application to Angular (i.e. Angular 4.1.3). I chose to do the incremental upgrade so currently both AngularJS and Angular are bootstrapped and running. So far so good, but: One of the AngularJS services that should be rewritten to Angular relies on a well known service $translate
which is part of the 3rd party AngularJS (read Angular 1) module pascalprecht.translate. In other words, $translate
service is injected to MyService
which should become an Angular (read Angular 4) service.
MyService
(stripped down) looks like this:
import { Injectable } from '@angular/core';
@Injectable()
export class MyService {
// Here comes the injection (how to do it?).
constructor(private $translate: $translate) {
}
foo() {
// Use the service
this.$translate('HELLO_WORLD');
}
}
It is located within MyModule
:
import { NgModule } from '@angular/core';
import { MyService } from './my.service';
@NgModule({
providers: [
MyService
]
})
export class MyModule {
}
The problem
Now, how can I inject $translate
into MyService
when MyService
resides within an Angular module while $translate
is part of a 3rd party AngularJS module?
I know how to inject an AngularJS service into an Angular service if the AngularJS service is located within the same module (or at least the module is part of my own solution). It is explained in the official docs. Is there any way to handle a 3rd party service? Do I need to register that service within MyModule
's providers?
import { NgModule } from '@angular/core';
import { MyService } from './my.service';
// How this line should look line then?
import { $translate } from 'node_modules/angular-translate/...';
@NgModule({
providers: [
MyService,
$translate
]
})
export class MyModule {
}
Or am I trying to achieve impossible?