I'm working around the dependency injection in angular 2. I had written a sample service which will not contain any constructor My service :
import {Injectable} from '@angular/core'
@Injectable()
export class MyFirstServiceClass{
//Not working with parameter
// constructor(nm:string){
// }
SayHello(){
alert();
return "Hello, I'm Service...";
}
}
I accessed the service via dependency injection from the constructor as follows :
import { Component } from '@angular/core';
import {MyFirstServiceClass} from './MyFirstService.service'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
Assignonclick : string;
constructor(public refsrvc : MyFirstServiceClass){
this.title = refsrvc.SayHello();
}
CallServiceOnBtnClick(){
this.Assignonclick = this.refsrvc.SayHello();
}
}
It's working if I didnt provide a parameter to the constructor of my service class. In case I do the following as like providing the parameter in the constructor, I was not able to perform DI.
@Injectable()
export class MyFirstServiceClass{
//Not working with parameter
constructor(nm:string){
}
SayHello(){
alert();
return "Hello, I'm Service...";
}
}
This is providing me the following image straight forward error :
