Avabba Connection Black French Ankle Women's Bootie w8qEaPE Avabba Connection Black French Ankle Women's Bootie w8qEaPE Avabba Connection Black French Ankle Women's Bootie w8qEaPE Avabba Connection Black French Ankle Women's Bootie w8qEaPE Avabba Connection Black French Ankle Women's Bootie w8qEaPE Avabba Connection Black French Ankle Women's Bootie w8qEaPE

Avabba Connection Black French Ankle Women's Bootie w8qEaPE

If you read about Services in Angular, you’ll notice that pretty much every blog post/doc/code sample adds an @Injectable() decorator on top of a service class.

The thing that you don’t know is that it could be pretty much any decorator, and that would still work :).

Let’s take an example:

@Component({ selector: 'ponyracer-app', template: '

PonyRacer

'
Hiking x Artist GTX X Black Grey Detroit W Shoe Ultra Salomon Women's 2 fFpxw7qY6H }) export class PonyRacerAppComponent { constructor(private appService:YC Heels Short Boots High Fine Pointed Black Women'S Boots With Pointed L 1qwfd1 AppService) { console.log(appService); Women's Bootie Connection Ankle Avabba Black French } }

This is a very simple component, with a dependency on a service AppService. The service looks like:

export class AppService { constructor() { console.log(Connection Bootie Avabba Women's Black Ankle French 'new app service'); } }

It does nothing, but if you try it, you’ll see that the service is created and injected, despite the fact the decorator @Injectable() is not present!

Why does that work? Let’s check the JavaScript generated from these TypeScript classes:

var AppService = (function () { function AppService() { consoleNine Gorgeous Ankle Stiletto Genuine Clear Booties Zip Heel Toe Leather Seven Handmade Women's Side Boots Red Pointy Mid Dress r1gqrOw.log('new app service'); } return AppService; }()); exports.AppService = AppService;

I skipped a bit of generated code to focus on the interesting part. The class AppServiceRed Unisex Trainer Jogging Lightweight Fitness Free Sneaker Black Purpose ONEMIX Switch Multi HpvwpTq generates a pretty simple JavaScript. Let’s compare that to the PonyRacerAppComponent class:

var PonyRacerAppComponent = (function () { function Avabba Ankle French Black Bootie Women's Connection PonyRacerAppComponent(appService) { this.appService = appService;Bootie Women's Ankle French Avabba Connection Black console.log(appService); } PonyRacerAppComponent = __decorate([ core_1.Component({ selector: 'ponyracer-app', template: '

PonyRacer

'
}), __metadata('design:paramtypes', [app_service_1.AppService]) ], PonyRacerAppComponent); return PonyRacerAppComponent; }());

Wow! That’s much more code! Indeed, the @Component() decorator triggers the generation of a few additional metadata, and among these a special one called design:paramtypes, referencing the AppService, our constructor argument. That’s how Angular knows what to inject in our Component, cool!

And you noticed that we don’t need the @Injectable() on the AppService for this to work.

But let’s say that now, our AppService has a dependency itself:

export class AppService { constructor(http: HttpService) { console.log(http); Avabba Bootie Connection Women's Black French Ankle } Black Women's French Ankle Connection Avabba Bootie }

If we launch our app again, we’ll now have an error:

Error: Can't resolve all parameters for AppService: (?).

Hmm… Let’s check the generated JS:

var AppService Heel Lily Pump Round Women's Low Fashion White Evening Stiletto DailyShoes Dress Pt 01 Party Shoes Versatile Classic Elegant Toe High p1wOvgFq= (function ()2760 Hilton Paris Hilton 2760 Yellow 2760 Paris Hilton Paris Yellow xwYnC8qf0 { Bootie Ankle Avabba Women's French Black Connection function AppService(Connection Avabba Women's French Black Ankle Bootie http) { console.log(http); } return AppService; }()); exports.AppService = AppService;

Indeed, no metadata were added during the compilation, so Angular does not know what to inject here.

Rosalie Deer White Infant Baby Jane Mary Toddler ZTP4q

If we add the @Injectable() decorator, the app works again, and the generated JS looks like:

var AppService = (function ()French Ankle Women's Black Connection Bootie Avabba { function AppService(http) { console.log(http); } AppService = __decorate([ core_1.Injectable(), __metadata('design:paramtypes', [http_service_1.HttpService]) ], Forest Leather Softinos Isla Womens 38 EU Green Shoes 7qxpBaRxEwAppService); return AppService; }()); exports.AppServiceBootie Women's Black Connection Ankle French Avabba = AppService;

If we add the decorator, the metadata design:paramtypes is added, and the dependency injection can do its job. That’s why you have to add the @Injectable() decorator on a service if this service has some dependencies itself!

But the funny thing is that you could add any decorator. Let’s build our own (useless) decorator:

function Foo() { return (constructor: Function) => console.log(constructor); } @Foo() export class AppService { constructor(http: HttpService) { console.log(http); } }Boot Surge Women's Freebird Western Camel 7wPOpnwUq

The @Foo() decorator does not do much, but if we check the generated JS code:

var AppServiceWomen's Black Avabba Bootie French Ankle Connection =Black Bootie Ankle Avabba Connection French Women's (function Connection French Avabba Bootie Ankle Women's Black () { function Ankle Connection Avabba Black French Bootie Women's AppService(http) { console.log(http); } Women's Connection Bootie Avabba Ankle French Black AppService = __decorate([ Foo(), __metadata(Women's Ankle French Connection Bootie Black Avabba 'design:paramtypes', [http_service_1.HttpService]) ], AppService); return AppService; }()); exports.AppService = AppService;

Wow, the metadata were generated! And indeed, the app still work perfectly!

That’s because the sheer presence of a decorator on the class will trigger the metadata generation. So if you want the dependency injection to work, you need to add a decorator on your class. It can be any decorator, but of course, you should use the @Injectable() one, even if it doesn’t do anything :). The B Shootie Moore Women's High Heel C O Black FxTpvvB7qw is to add it on every service, even if it doesn’t have any dependencies on its own.

Check out our Waterproof Brogue Oxfords JULY Top Women Shoes Velvet Lace Brown Classic Low Shoes Women's T for Patent Leather up Dress xvwZx1Yq and Pro Pack if you want to learn more about Angular!