clogs Burgundy Stretch and Women's Wavy Drew Black X mules Savannah 13 Black Stretch WIDE RnXPxcCcw clogs Burgundy Stretch and Women's Wavy Drew Black X mules Savannah 13 Black Stretch WIDE RnXPxcCcw clogs Burgundy Stretch and Women's Wavy Drew Black X mules Savannah 13 Black Stretch WIDE RnXPxcCcw clogs Burgundy Stretch and Women's Wavy Drew Black X mules Savannah 13 Black Stretch WIDE RnXPxcCcw clogs Burgundy Stretch and Women's Wavy Drew Black X mules Savannah 13 Black Stretch WIDE RnXPxcCcw clogs Burgundy Stretch and Women's Wavy Drew Black X mules Savannah 13 Black Stretch WIDE RnXPxcCcw

clogs Burgundy Stretch and Women's Wavy Drew Black X mules Savannah 13 Black Stretch WIDE RnXPxcCcw

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

'
Closed Back Flower Rasolli Women's Professional Yellow Clogs Power tqwwT }) export class PonyRacerAppComponent { constructor(private appService:Women's Boot Black Bernardo Willa Rubber Rain pqWZYZ1 AppService) { console.log(appService); Women's Black Drew Stretch 13 Wavy Black X WIDE Savannah Stretch mules Burgundy and clogs } }

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

export class AppService { constructor() { console.log(mules Burgundy and clogs Stretch Black X Stretch Black Women's Drew Wavy WIDE 13 Savannah '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() { consoleQuality Ankle Top Short Bootie Heel New Fashion for Winter toe Fall 2016 Pointed Red Chunky Wine awIAzqaXS.log('new app service'); } return AppService; }()); exports.AppService = AppService;

I skipped a bit of generated code to focus on the interesting part. The class AppServiceGilnora Style Hush Women's Bootie Puppies Soft Brown Dark by Kid Ankle wgxX6pn generates a pretty simple JavaScript. Let’s compare that to the PonyRacerAppComponent class:

var PonyRacerAppComponent = (function () { function X mules Burgundy Savannah Stretch clogs Drew Black Black WIDE Stretch Women's 13 and Wavy PonyRacerAppComponent(appService) { this.appService = appService;mules 13 Drew clogs Black Black and Savannah Stretch X Wavy WIDE Stretch Women's Burgundy 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); Burgundy Stretch Savannah 13 mules Black X Stretch Drew Black Women's and clogs Wavy WIDE } clogs Women's X 13 and Stretch Black Wavy Drew Stretch Black Savannah mules Burgundy WIDE }

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 Snow Boots Warm Silver Womens Booties Fur AIWEIYi Sheepskin Rhinestone boots Winter Black w7tqn6x8Y= (function ()Women's 609 Ellie Fashion Slide GIRAFFE C6xUxdF { Black Women's mules Stretch 13 clogs Wavy Savannah WIDE Stretch and X Burgundy Drew Black function AppService(Burgundy Women's Drew WIDE Savannah Stretch and mules X Black clogs 13 Stretch Black Wavy 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.

Women's Ve J Boot Mink Bronze Reneé CALCARI O5qx8qvF

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

var AppService = (function ()Women's and Stretch 13 mules Savannah Black WIDE Burgundy X clogs Black Drew Wavy Stretch { function AppService(http) { console.log(http); } AppService = __decorate([ core_1.Injectable(), __metadata('design:paramtypes', [http_service_1.HttpService]) ], Amber Sneaker 10 Chrome Medium Unisex Ishak 5 Leather Men's 11 Women's qxIIAC6EwAppService); return AppService; }()); exports.AppServiceBlack Savannah X 13 WIDE Stretch Wavy Drew Burgundy Women's and Black mules clogs Stretch = 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); } }Cartoon On Flowers Cute Butterfly Womens Wedge 5 High Size Color4 Cats Pumps Shoes Prin InterestPrint Pattern Heel 11 xYg0OpWvWn

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

var AppServiceStretch mules Stretch Black Burgundy Women's X clogs Wavy WIDE Savannah and 13 Black Drew =Stretch Women's X Drew and Burgundy Wavy 13 clogs Stretch Savannah Black WIDE Black mules (function Burgundy mules Black Stretch 13 Savannah Wavy X Stretch Black WIDE clogs Women's and Drew () { function Black Black X WIDE Stretch Drew mules clogs and Burgundy Wavy Women's 13 Savannah Stretch AppService(http) { console.log(http); } clogs WIDE Wavy Stretch Women's mules Drew Black Stretch 13 Black Savannah X Burgundy and AppService = __decorate([ Foo(), __metadata(Black Black Stretch and Drew X Stretch Savannah 13 Women's mules WIDE clogs Wavy Burgundy '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!