angular2 injecting service to a pipe

时间:2016-04-12 00:39:25

标签: angular

Does anyone know how to injecting service to a Pipe in angular2? I've got an example in Plunkr

So currently the service is doing nothing. I've put the service in the constructor of the pipe, and put the Http in the Provider of app. Instead I got an error saying

ChangeDetectionError {_wrapperMessage: "No provider for ConnectionBackend! (MyPipe -> MySe…Backend) in [Hello {{name | MyPipe}} in App@2:10]", _originalException: NoProviderError, _originalStack: "Error: DI Exception↵    at NoProviderError.BaseExc…ularjs.org/2.0.0-beta.8/angular2.dev.js:11284:19)", _context: _Context, _wrapperStack: "Error: No provider for ConnectionBackend! (MyPipe …gularjs.org/2.0.0-beta.8/angular2.dev.js:12682:27"…}

I'm not really sure what else I need to do. I'm using angular2 beta 8. Help will be much appreciated.

Thanks

2 个答案:

答案 0 :(得分:1)

您应该添加http提供商:

import {HTTP_PROVIDERS} from 'angular2/http';

providers: [MyService,HTTP_PROVIDERS]

我修好了你的插件here

答案 1 :(得分:1)

Updated plunk

您需要提供HTTP_PROVIDERS才能将Http注入MyService

MyService也需要使用Injectable()进行修饰。