0
votes

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 :

enter image description here How can I perform DI with parameter ?

3
Why are you passing the string to the constructor? Why can't you use another method to act as getter/setter - cyberpirate92
It's like assumption as like IF it expects a parameter : Then how to handle DI? - Srikanth Reddy

3 Answers

2
votes

Because you want DI mechanism to create for you the instance of MyFirstServiceClass - DI mechanism must know anything about the constructor parameters of that injectable service.

When you do this

constructor(public refsrvc : MyFirstServiceClass){

}

DI knows that there is something to inject with type MyFirstServiceClass, so it tries to inject it. But when it tries access the constructor of that class - at that moment DI knows nothing about parameter nm:string so why it can't create an instance of the MyFirstServiceClass. So based on the type of your parameter which is string it gives you correct error

No Provider for String

0
votes

Your service:

@Injectable()
export class MyFirstServiceClass{
  nm: string;

  constructor(nm:string){
    this.nm = nm;
  }
  SayHello(){
    alert();
      return "Hello, I'm Service...";
  }
}

When you want to use it:

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';
  refsrvc : MyFirstServiceClass
  Assignonclick : string;

  constructor() {
    this.refsrvc = new MyFirstServiceClass('The string of the constructor');
    this.title = this.refsrvc.SayHello();
  }

  CallServiceOnBtnClick(){
    this.Assignonclick = this.refsrvc.SayHello();
  }
}
0
votes

You need to pass parameter or either use get/set, if you have defined parameter in the constructor class of the service.