Replies: 61 comments 225 replies
-
Thank you. That is long due. When we created the style guide in 2016, we only had angular.js ideas in mind and an idea of what Angular would be. We were right in certain things, we were wrong in other things. In any case, that was 8 years ago and I think everything changed (for the better) |
Beta Was this translation helpful? Give feedback.
-
Thank you; this is long overdue. The suffix is pointless and actually means that if you want to import it only once and append the component to the class name, the compiler will have to remove the suffix, which isn't ideal. @jelbourn any thoughts on DI, i think with update to guild Angular needs to take a position on using DI or the new inject service locator |
Beta Was this translation helpful? Give feedback.
-
It would be nice to have a place that group all Angular best practices, with anchor, same as the current guide. |
Beta Was this translation helpful? Give feedback.
-
It might be cool to explain what hook you will need depending on what you are trying to do. The thing I see the most are people using The same goes for |
Beta Was this translation helpful? Give feedback.
-
I think it would be helpful to cover following topics:
Not sure it's 100% Angular-related, but I think it would be valuable to have such guidance. |
Beta Was this translation helpful? Give feedback.
-
Regarding suffixes in class names, I understand the rationale for reusable components that are part of a component library such as Angular Material for example: On the other hand, for application components whose main role is to display data, if we follow the advice in the new style guide, we'll end up with components such as I'm afraid that to comply to the style guide, we end up with TypeScript interfaces named as So I'd find it helpful to keep the |
Beta Was this translation helpful? Give feedback.
-
I don't fully understand the recommendation to avoid services, components, directives, etc., folders. In my opinion, these are often quite useful. I feel the guide lacks a more extensive example that demonstrates a complete application structure. One that includes multiple directives, pipes, services, components, etc., because right now, I better understand what to avoid rather than what is recommended when it comes to organizing folders within src. |
Beta Was this translation helpful? Give feedback.
-
I feel that this style guide is now less of a guide and more of a standards. And I could even see a guide that goes on top of this for more recommendations. But if the goal is to match all projects, I think it would be helpful for any style guides for specifics. A style guide for a CRUD application, or one for public websites, one that is an SPA or one that is a major application with many modules and many teams. Because these are different use cases that would seek guidance on how the Angular team recommends they work at. Modules are mostly removed but their separation is still valid. Also, I wouldn't mind a more strict recommended project setup where you go into detail what would be recommended for spacing, what goes on a new line and what doesn't, where you place comma's and stuff like that. We already have various ESLint plugins and even Angular ones that I think are good for people to use, but what would be the recommendation for making code look the same. Because ultimately I think the goal of these style guides is to get code in a similar style so that anybody can work on an Angular project. Which to me is the main reason to use Angular: because its applications all look similar enough. Right now I have a lot of rules and settings to make sure linters automatically format most of the code and recommend certain changes to make code seem similar. Having a style guide from Angular that brings code to a higher level, would seem like a good idea to me. Even though some folks will be annoyed by a few of the recommendations, the thought of a code standard that is preferred by the team, would greatly benefit the ecosystem. Similarly it doesn't say anything about TSConfig (or its setup) or what you should do with the angular.json configuration. And I feel it should also go into more detail on the testing part. Both unit testing and E2E/integration testing. |
Beta Was this translation helpful? Give feedback.
-
While Angular is well-known for being an opinionated framework, I observe a significant lack of documentation regarding best practices. Personally, I follow the Angular team on social media, and while it's clear to some developers that certain practices are preferred, these are not officially documented. For example, in a discussion at work, I had to convince my colleagues that OnPush is always preferable in modern Angular. Some argued that this preference isn't stated anywhere in official documentation and Angular itself does not come with OnPush as the default strategy. Additional examples of areas where official recommendations would be beneficial include:
Providing clear, official guidance on these and similar topics would greatly benefit the community by reducing ambiguity and helping teams adopt best practices more consistently. |
Beta Was this translation helpful? Give feedback.
-
Just a heads-up on this matter. Since Angular templates are no longer "pure HTML" due to the new control-flow syntax, Prettier relies on the
|
Beta Was this translation helpful? Give feedback.
-
Sometimes, a simple expression is not enough. Will there be a recommendation when the logic required is more complex than teh basic expression parser for the Extending from that...any chance we might get a function to create host bindings when they need to be more complex? Just, really really rough, off the top of my head, maybe: @Component({ ... })
export class AdvancedWidget {
#complexBinding1 = hostBinding(value => /* complex logic here? */, 'atter.whatever');
#keydownHandler = hostListener(($event: Event) => /* complex logic here? */, 'keydown');
} This would be in-line with the new functional input/output/signals stuff, and support whatever level of complexity is necessary with full support for TypeScript's featureset? I feel this would be a better approach, than say...expanding the angular parser for the |
Beta Was this translation helpful? Give feedback.
-
Introduction
Angular's current style guide was originally written in 2016, just as v2.0.0 released. A lot has
changed in the intervening eight years.
This RFC proposes a significant overhaul of Angular's style guide
with a handful of primary goals:
help ourselves)
As always, keep our code of conduct in mind. We know that a proposal of this scope affects the way the community writes Angular code, and that people have invested their time and energy learning these practices in the past. We appreciate your commitment and investment in this project and ask to keep comments respectful. Remember, we are all just folks doing our best with limited resources.
📜 Full draft of the revised style guide
What kind of feedback are we looking for?
We're happy to take any and all feedback on this update. However, we're primarily interested in
the following:
these changes?
Notable changes
Remove general code health guidance that's not meaningfully related to Angular
The current style guide includes a significant amount of advice relating to general coding best
practices. Examples include:
While these are reasonable best practices, they're not really specific to Angular. There's a massive
corpus of books, articles, and videos dedicated to general coding best practices; we believe that
the Angular style guide will be more useful focused on framework-specific practices without trying
to curate a subset of these more general practices.
The revised guide still includes some general advice, mostly where such advice brushes close to a
particular aspect of writing Angular code.
Move Angular best practices to appropriate documentation instead of the style guide
We want the style guide to focus on coding style. Much of the content in the existing style
guide instead relates to Angular best practices. For the most part, this best practice advice has
moved (or will move) to the appropriate documentation page.
For example, guidance on choosing component selectors has moved to
the documentation page for component selector.
Stop recommending suffixing most file names and class names with the type of construct
The current style guide recommends suffixing file names and class names with the type of Angular
construct. For example:
user-profile.component.ts
andUserProfileComponent
campaign-data.service.ts
andCampaignDataService
Over time, we've observed that this naming convention can make the framework feel cumbersome and
boilerplate-y, especially for new developers unaccustomed to this practice.
For class naming, we believe it's better for a class name to reflect the class's behavior and
responsibilities, not how it's used. The term "service", in particular, does not add any
meaningful information to explain what a class does. Thus, this naming convention can sometimes lead
to less helpful names for classes. For example, a name like
UserDataClient
tells you much moreabout what a class does than
UserService
. As a result of our observations, we're reversingthis guidance.
There are two exceptions to this change: Pipes and NgModules.
sense to keep class names like "DatePipe". As with
NgModule
, the file name wouldbecome
date-pipe.ts
.Remove (almost) all guidance related to
NgModule
A large portion of the current style guide pertains to
NgModule
— when to use them, how to organizethem, etc. The Angular team now recommends using standalone components and
providedIn: 'root'
over
NgModule
, so this guidance can be removed from future versions of the style guide. All of thehistoric
NgModule
guidance will remain accessible on the archived versionsof angular.dev.
Remove suggestion to put templates in a separate file
The current style guide recommends extracting component templates to a template file if they exceed
three lines. Since this was written, the popularity of both JSX-based template styles and
single-file component formats has exploded. Because of this change in the broader ecosystem, we no
longer see it as necessary to strictly recommend moving templates to a separate file.
This change does not mean that inline templates will be recommended- the change is that separate
files and inline templates are both acceptable. You can still have an opinion for your projects, but
the Angular team no longer takes a stance here.
Stop recommending
@HostBinding
and@HostListener
The current style guide recommends
@HostBinding
and@HostListener
over thehost
property inthe
@Component
and@Directive
decorator. The updated style guide reverses this advice.Astute readers may have already noticed this updated guidance in the rewritten Components guide
on angular.dev and discussion on this topic in the signal-based components RFC.
There are a number of reasons for this change:
decorators, making
@HostBinding
and@HostListener
somewhat misaligned with this more modernAPI style.
host
object matches the syntax used in the template, leading to a more consistentexperience.
host
property is easier to quickly scan. CSS classes alone aremuch easier to reason about when they're all defined in one place, and seeing ARIA attributes
all together helps sanity check that the right attributes are applied.
HostListener
doesn't support expressions, which encourages developers to create unnecessaryone-line methods.
HostBinding
doesn't allow for static values (e.g.role
andtabindex
), leading to unnecessarydynamic bindings.
One downside to the
host
object is that Angular does not yet do any type-checking on expressionsin
host
bindings. The team is acutely aware of this gap and plans to add type checking forhost
in the not-too-distant future.
Stop recommending the "on" prefix for event handlers
The current style guide recommends prefixing event handler names with "on", such as
onClick
or
onKeydown
. The updated advice is to name methods for what they do rather than when they'recalled.
Frequently asked questions
Why change the style guide at all?
**Angular has evolved a lot! ** Since the style guide was written, we've moved from
NgModule
tostandalone components, introduced signals, new control flow syntax, and so much more.
**We've seen eight years worth of Angular codebases! ** The current style guide was written when
Angular was still quite new. As we've seen so much Angular code written over the years, we've had
time to rethink some of the practices recommended in the guide.
The web ecosystem has changed a lot! Developers had certain notions and preferences around web
development in 2016 that have shifted over time. For example, the popularity of the single file
component authoring style in frameworks like Vue and Svelte have demonstrated that opinions have
shifted about separating code and templates into separate files.
But I like the old way!
That's okay! Ultimately the style guide is just a set of recommendations- the framework will still
work fine if you decide to stick with the original conventions. Historic versions of the style guide
will remain available in the archived versions of angular.dev. But we think
the updates proposed here constitute a better set of recommendations for new code going forward.
Will community tools (like angular-eslint) support the new recommendations?
We plan to coordinate with community supported tool owners so that they can be updated alongside
the revised style guide becoming official.
Will this new style guide land in version 19?
No— these changes aren't ready for v19, so they'll land in a later version.
Why doesn't the guide recommend signals?
While signals are maturing rapidly, some APIs remain in developer preview and some parts of the
ecosystem (forms, router) have yet to receive a signals makeover. We'll do continue to update the
guide as signals continue to mature.
Will you update the code generated by
ng new
?Yes- we consider updating the code generated by
ng new
to be a requirement for publishing updatesto the style guide.
Will you provide schematics to refactor code to follow the new guidance?
We plan to offer optional refactoring schematics to update code to the new guidelines where it's
feasible. These schematics would be opt-in and not automatically run as part of
ng update
.Beta Was this translation helpful? Give feedback.
All reactions