Before implementing new functionality, check if Angular already provides a suitable utility, service, or pattern that can be reused. This promotes consistency across the codebase, reduces duplication, and follows established Angular conventions.
Before implementing new functionality, check if Angular already provides a suitable utility, service, or pattern that can be reused. This promotes consistency across the codebase, reduces duplication, and follows established Angular conventions.
Key areas to check:
Clipboard
service instead of implementing custom clipboard functionalityng
global APIs for devtools functionality rather than creating new approachesretrieveTransferredState()
instead of reimplementing similar logicExample of good practice:
// Instead of custom clipboard implementation
async copyToClipboard(text: string) {
await navigator.clipboard.writeText(text);
}
// Use Angular's Clipboard service
constructor(private clipboard = inject(Clipboard)) {}
copyToClipboard(text: string) {
this.clipboard.copy(text);
}
// Instead of separate provider
export const WINDOW_PROVIDER: Provider = {
provide: WINDOW,
useValue: window,
};
// Use factory in token definition
export const WINDOW = new InjectionToken<Window>('WINDOW', {
factory: () => window,
});
This approach ensures better maintainability, leverages Angular’s built-in optimizations, and provides a more consistent developer experience.
Enter the URL of a public GitHub repository