Angular Components
ASecurityPrefer `toSignal()` for an observable consumed by the template. It subscribes in the component's injection context and automatically unsubscribes when the component is destroyed. ```ts import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; import { toSignal } from '@angular/core/rxjs-interop'; @Component({ selector: 'app-user-feed', standalone: true, imports: [], changeDetection: ChangeDetectionStrategy.OnPush, template: ` @for (user of users(); track user.id) { <p>{{ use...
- 584 stars
- 0 votes
- 0 copies
- 2 views
- Added September 5, 2026
Security analysis
100/100Pro scans all 21 files and shows the line behind each finding
npx -y skills add HoangNguyen0403/agent-skills-standard --skill angular-components --agent claude-codeAre you the author of Angular Components?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-angular-components-ef9bb1c8)# Avoiding subscription memory leaks
Prefer `toSignal()` for an observable consumed by the template. It subscribes in the component's injection context and automatically unsubscribes when the component is destroyed.
```ts
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
@Component({
selector: 'app-user-feed',
standalone: true,
imports: [],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@for (user of users(); track user.id) {
<p>{{ user.name }}</p>
} @empty {
<p>No users.</p>
}
`,
})
export class UserFeedComponent {
private readonly userService = inject(UserService);
readonly users = toSignal(this.userService.users$, { initialValue: [] });
}
```
If an imperative side effect genuinely requires `subscribe()`, bind its lifetime with `takeUntilDestroyed()`:
```ts
import { DestroyRef, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
export class EventConsumerComponent {
private readonly eventService = inject(EventService);
private readonly destroyRef = inject(DestroyRef);
constructor() {
this.eventService.events$
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((event) => this.handleEvent(event));
}
private handleEvent(event: EventPayload): void {
// Perform the required imperative side effect here.
}
}
```
The `async` pipe is another safe template option. Do not leave a raw `subscribe()` unmanaged; use `toSignal()`, the `async` pipe, `takeUntilDestroyed()`, or an equivalent `DestroyRef` cleanup. Keep the component standalone and OnPush, and reserve `effect()` for intentional signal side effects rather than replacing ordinary derived state with subscriptions.
Files in this skill
- eval-1.baseline.md
- eval-1.with-skill.md
- eval-2.baseline.md
- eval-2.with-skill.md
- eval-3.baseline.md
- eval-3.with-skill.md
- eval-4.baseline.md
- eval-4.with-skill.md
- eval-5.baseline.md
- eval-5.with-skill.md
- eval-6.baseline.md
- eval-6.with-skill.md
- eval-7.baseline.md
- eval-7.with-skill.md
- eval-8.baseline.md
- eval-8.with-skill.md
- eval-9.baseline.md
- eval-9.with-skill.md
- trigger-1.md
- trigger-2.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.