Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject services in another service angular 2

I tried injecting one service in another using DI of angular 2

import {HttpService} from 'scripts/httpService';
export class CurrentBlog{
    constructor(public httpService:HttpService){}
}

When I do this, I get this error :

Cannot resolve all parameters for CurrentBlog(?). Make sure they all have valid type or annotations.

I have tested DI with normal components and it works fine. But when I inject it in service. It simply doesn't work.

like image 835
binariedMe Avatar asked Nov 10 '22 00:11

binariedMe


1 Answers

In angular 2 you need to make the angular injector aware of your service. To do this you need to mark the service as Injectable.

HttpService

import {Injectable} from 'angular2/angular2';

@Injectable()
export class HttpService{
    ...
}

CurrentBlog

import {HttpService} from 'scripts/httpService';
import {Inject} from 'angular2/core';

export class CurrentBlog{

    constructor(public httpService:HttpService){}
}
like image 100
SnareChops Avatar answered Nov 15 '22 07:11

SnareChops