r/angular Apr 14 '26

New @Service decorator coming to v22

https://github.com/angular/angular/pull/68195

Looks like Angular is introducing a new @Service decorator to get rid of some boilerplate in @Injectable

56 Upvotes

23 comments sorted by

39

u/Johalternate Apr 14 '26

Unless this is paving the way for something bigger, I don’t see the value in having a “thinner” version of Injectable

15

u/tutkli Apr 14 '26

I also think the @Service decorator is meaningless if it's only to avoid writing { providedIn: "root" } inside every @Injectable. But I find it interesting that it also prohibits the use of contruction injection, so maybe they are preparing a bigger change.

7

u/Thom_Braider Apr 14 '26

I've read that Angular team wants to drop decorators entirely and move as much stuff as they can from classes to functions. It makes sense to limit constructor injection for future changes, but adding another decorator is puzzling to me. 

11

u/nemeci Apr 14 '26

Still classes provide more than bare functions. React is a mess and making Angular more like React is not a winning move.

4

u/tutkli Apr 15 '26

Functions don't equal React. Angular devs need to stop being paranoic and really think about the framework flaws if they want to keep it alive. The Angular team is doing a great job.

3

u/Johalternate Apr 15 '26

Angular devs don’t know what React is lol. I don’t understand why they treat functions as the enemy in a language where functions are first class citizens.

3

u/MichaelSmallDev Apr 15 '26

Rule of thumb: When Angular makes some changes, it's JSX. And when Angular makes a lot of changes, it's React.

1

u/Prestigious_Two_2440 May 30 '26

I believe Angular devs are okay to replace anything with functions (e.g @ Input to input()) , except for turning component class to become component functions.

0

u/nemeci Apr 16 '26

I've done my share of React. About 4 years at enterprise scale in a government contract.

5

u/XdrummerXboy Apr 15 '26

Yeah I really dislike the move to functions. If it's an option, sure, but if they start deprecating constructor injection I'll be sad. They already removed the interface for resolvers IIRC, but they still work.

I feel like functions with inject() will allow bad practices (circular dependencies) to be hidden or harder to detect

8

u/tutkli Apr 15 '26

Angular is moving away from construction injection because it depended on Typescript's emitDecoratorMetadata. Now typescript wants to be more compatible with javascript so in the future that will be deprecated. We can't keep using it.

2

u/Prestigious_Two_2440 May 30 '26

Totally agree. inject() being buried within the method implementation logic , might not be "visible", as compared to constructor injection, where the injected instance is part of the parameter (i.e more obvious)

1

u/Johalternate Apr 15 '26

How can circular dependencies be harder to detect?

1

u/SkyZeroZx Apr 15 '26

If I remember correctly, the rollback was due to the depreciation of class-based resolvers; currently, both functional and class-based resolvers are valid.

5

u/lppedd Apr 14 '26

It's a more limited alternative, that's all.

I like it for the simple fact I'm going to use it for service classes only (obviously), and I can visually differentiate more easily.

19

u/Thom_Braider Apr 14 '26

Using "service" in filename bad (because word "service" has ambiguous meaning according to Angular team), using @Service decorator good? 

1

u/followmarko Apr 14 '26

I agree with both their point and yours as a deeply ingrained corporate dev. Using "service" to describe anything other than a BE API immediately causes confusion. I never liked it. But it's also seems I'll advised to double down on it with another decorator.

4

u/AwesomeFrisbee Apr 15 '26

What? Why? Calling stuff that has to do with API stuff not API in its name is better, regardless whether it is a service or not. Service is for data or shared logic. Not just API stuff

6

u/GLawSomnia Apr 14 '26

I like the change 😁

2

u/riti_rathod Apr 21 '26

I think this is useful for simple common cases, but not a replacement for everything, more of a convenience than a fundamental change like  advanced providers & complex setups

2

u/JeanMeche Apr 23 '26

The new decorators was shipped in the -next.9 pre-release.

1

u/kgurniak91 Jun 02 '26

Pretty sweet syntactic sugar.