I'm trying to get a very basic, functional structure set up for Angular 2. It will have only the most basic API elements so that as the framework advances, I can advance my structure.
Currently, I'm at my wit's end as to how to perform the simple act of passing services. Here is some example source, taken right from the comments of the most recent DefinitelyTyped file:
class Greeter {
greet(name: string) {
return 'Hello ' + name + '!';
}
}
@Component({
selector: 'greet',
appInjector: [Greeter]
})
@View({
template: `{{greeter.greet('world')}}!`
})
class HelloWorld {
greeter: Greeter;
constructor(greeter: Greeter) {
this.greeter = greeter;
}
}
bootstrap(HelloWorld);
As you can see, I'm using Typescript 1.5 in this example.
I have tried to inject the Greeting
service in the Component annotation using hostInjector
, injectibles
and appInjector
. I've also tried adding it to the second argument of the bootstrap call, as in bootstrap(HelloWorld, [Greeter])
.
In all cases I get this error message when trying to run it in the browser:
Error during instantiation of Token(ComponentRef)!. ORIGINAL ERROR: Cannot resolve all parameters for HelloWorld(?). Make sure they all have valid type or annotations.
Of course, if I remove greeter: Greeter
argument from the constructor, the error in question goes away and is replaced by other, expected errors further down the chain.
Ideas?
EDIT
I updated the title of this question to specify that I am using TypeScript 1.5
Here is the code, straight out of the plnkr.co template for injecting a service http://plnkr.co/edit/m5sHWWFmgYPrfsMv0u2r