Your are trying to use modal for your code , just for type checking , if you are using as you used in question you don't need to use @inject,
Better way to create single service in which you can check types, call methods and do whatever you want, now if you are going to create a service than it is necessary to provide at least one decorator, either you can @Component
or you can use @Injectable
. like this -
import { Injectable } from './@angular/core';
@Injectable()
export class TestService {
name:string =null;
email:string =null;
...///
constructor(){
//do your stuff
}
}
or you can also use this like this -
import { Component } from './@angular/core';
@Component({
selector: 'testService'
})
export class TestService {
name:string =null;
email:string =null;
...///
constructor(){
//do your stuff
}
}
PS: - It is necessary to decorate class with at least one decorator
now by doing so you have successfully create one class, now if this class is being used in multiple components you can inject this at the time of bootstrap like this -
bootstrap(AppComponent, [TestService]).catch(err => console.error(err));
now if you inject your service while bootstrap, you dont need to provide this every time in the list of providers because angular automatically create instance of this service for each component,
But if you have't Inject your service while bootstrap than you need to import your service and also need to inject in the list of providers than you are able to use it like this : -
import { Component } from './@angular/core';
import { TestService } from './TestService';
@Component({
selector: 'testService',
providers: [TestService]
})
export class AppComponent {
constructor(private service : TestService){
this.service.blabla //now you are able to use your service here
}
}
Hope this one clear some points regarding Injection in Angular2.