Reputation: 1055
I have a service like the following:
import {Injectable} from 'angular2/core';
import {Router, RouteParams} from 'angular2/router';
@Injectable()
export class QService {
/* A service to handle a query (q) in the search string.
*/
constructor(
private _router:Router,
routeParams:RouteParams
) {
/* Set this._q from the search string.
*/
this._q = routeParams.get('q');
}
private _q:string
get q():string {
return this._q;
}
set q(q:string) {
this._q = q;
// TODO Add q back to the search string.
}
}
Unfortunately, no matter how I use this service, I get an error along the lines of No provider for RouteParams
. I'm stumped. Is there some recommended or simple way of doing this I've missed?
Upvotes: 2
Views: 607
Reputation: 202206
I think that you need to specify this service at the level of a component (involved in routing) within its providers attribute and not when bootstrapping your application:
@Component({
(...)
providers: [ QService]
})
export class...
As a matter of fact RouteParams only applies in the context of a component and not globally to the application. So the application injector doesn't have knowledge of this provider since it's present in a child injector only (a component one).
This is linked to dependency injection and hierarchical injectors. See this question for more details:
Upvotes: 1