Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sicherheitslücken (CVE)5 ways AI is reshaping the cybersecurity job market(21.09.2026 um 10:25 Uhr)
IT Security NachrichtenRevoking the token didn’t kill the backdoor(21.09.2026 um 11:00 Uhr)
Malware / Trojaner / VirenChainScript-RAT per Polygon: ClickFix-Kampagnen drehen C2-Infrastruktur(21.09.2026 um 10:55 Uhr)
IT Security NachrichtenEnterprise Mobile KI: So lassen sich Shadow-AI-Risiken kontrollieren(21.09.2026 um 12:00 Uhr)
Malware / Trojaner / VirenChainScript-RAT setzt auf Polygon-Blockchain für C2-Rotation(21.09.2026 um 12:19 Uhr)
Sicherheitslücken (CVE)5 ways AI is reshaping the cybersecurity job market(21.09.2026 um 10:25 Uhr)
IT Security NachrichtenRevoking the token didn’t kill the backdoor(21.09.2026 um 11:00 Uhr)
Malware / Trojaner / VirenChainScript-RAT per Polygon: ClickFix-Kampagnen drehen C2-Infrastruktur(21.09.2026 um 10:55 Uhr)
IT Security NachrichtenEnterprise Mobile KI: So lassen sich Shadow-AI-Risiken kontrollieren(21.09.2026 um 12:00 Uhr)
Malware / Trojaner / VirenChainScript-RAT setzt auf Polygon-Blockchain für C2-Rotation(21.09.2026 um 12:19 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Use effect less in Angular, try signals in computed

I watched a Youtube video where the Angular team lead, Alex Rickabaugh, discouraged the use of effect. Then, he demoed a way to replace effect with signals-in-computed that is not intuitive and would require developers to have a mental…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

I watched a Youtube video where the Angular team lead, Alex Rickabaugh, discouraged the use of effect. Then, he demoed a way to replace effect with signals-in-computed that is not intuitive and would require developers to have a mental shift of having writable signals in a computed signal.



Today, I would like to replace explicitEffect with signals and computed state.






Previous implementation of explicitEffect






searchId = signal(initialId); 
id = signal(initialId);
person = signal<undefined | Person>(undefined);
films = signal<string[]>([]);
rgb = signal('brown');
fontSize = computed(() => this.id() % 2 === 0 ? '1.25rem' : '1.75rem');






The component has some signals to store searchId, id, person, films and the random rgb code. The fontSize computed signal derives the font size based on the id.




 #logIDsEffect = explicitEffect([this.searchId],
([searchId]) => console.log('id ->', this.id(), 'searchID ->', searchId), { defer: true });

#rgbEffect = explicitEffect([this.rgb], ([rgb]) => console.log('rgb ->', rgb), { defer: true });

constructor() {
explicitEffect([this.id], ([id], onCleanUp) => {
const sub = getPersonMovies(id, this.injector)
.subscribe((result) => {
if (result) {
const [person, ...rest] = result;
this.person.set(person);
this.films.set(rest);
} else {
this.person.set(undefined);
this.films.set([]);
}

this.rgb.set(generateRGBCode());
this.rgb.set(generateRGBCode());
this.rgb.set(generateRGBCode());
});

this.renderer.setProperty(this.hostElement, 'style', `--main-font-size: ${this.fontSize()}`);
if (id !== this.searchId()) {
this.searchId.set(id);
}

onCleanUp(() => sub.unsubscribe());
});
}






The component has three effects that either log the signals in the console or update them. These signals need to be replaced with a computed state.






Replace the fontSize computed signal



After the code review, the process is to retain the id signal and eliminate the rest. The first step is to add a state computed signal and remove the fontSize computed signal.




state = computed(() => {
return {
fontSize: this.id() % 2 === 0 ? '1.25rem' : '1.75rem',
};
});






When the id signal updates, the state computed signal derives a new font size for the fontSize property.




host: {
'[style.--main-font-size]': 'state().fontSize',
},






Use the host property instead of using the Renderer2 and ElementRef to update the CSS variable.






Replace the rgb signal



state = computed(() => {

return {

fontSize: this.id() % 2 === 0 ? '1.25rem' : '1.75rem',

rgb: generateRGBCode(),

};

});



When the id signal changes, the state computed signal derives a new RGB code for the rgb property. Similarly, the host property also updates the CSS variable and I delete the #rgbEffect effect so that it does not log the rgb changes.




host: {
'[style.--main-color]': 'state().rgb',
},









Replace the searchId signal



The searchId signal needs more work than other signals. When the id signal updates, it also has the same value. When the searchId signal changes, the id signal also receives the latest value.




state = computed(() => {
const result = this.#personMovies();
return {
fontSize: this.id() % 2 === 0 ? '1.25rem' : '1.75rem',
rgb: generateRGBCode(),
searchId: signal(this.id()),
};
});






In the state computed signal, the searchId property is a signal with the initial value this.id(). When the id signal changes subsequently, the computed signal synchronizes the value of the searchId property.




syncId(id: number) {
if (id >= this.min && id <= this.max) {
this.state().searchId.set(id);
this.id.set(id);
}
}






When the user inputs a new id in the text field, the syncId method sets both the searchId property and the id signal.




<input type="number" [ngModel]="state().searchId()" (ngModelChange)="syncId($event)" />






The input field cannot use two-way data binding to bind the searchId signal to the ngModel directive. The ngModelChange event emitter invokes the syncId method to update signals.



In the constructor, delete the RxJS code because it is not used.




toObservable(this.searchId).pipe(
debounceTime(300),
distinctUntilChanged(),
filter((value) => value >= this.min && value <= this.max),
map((value) => Math.floor(value)),
takeUntilDestroyed(),
).subscribe((value) => this.id.set(value));






I prefer the above RxJS code over the syncId method; I would rather use the effect to synchronize the values of the id and searchId signals.






Use toSignal and toObservable to make HTTP request






function getPersonMovies(http: HttpClient) {
return function(source: Observable<Person>) {
return source.pipe(
mergeMap((person) => {
const urls = person?.films ?? [];
const filmTitles$ = urls.map((url) => http.get<{ title: string }>(url).pipe(
map(({ title }) => title),
catchError((err) => {
console.error(err);
return of('');
})
));

return forkJoin([Promise.resolve(person), ...filmTitles$]);
}),
catchError((err) => {
console.error(err);
return of(undefined);
}));
}
}






This is a custom RxJS operator to retrieve the details and films of a Star War character.




#personMovies = toSignal(toObservable(this.id)
.pipe(
switchMap((id) => this.http.get<Person>(`${URL}/${id}`)
.pipe(getPersonMovies(this.http))
),
), { initialValue: undefined });






The #personMovies uses the toSignal and toObservable functions to create a signal of Star War details. I feel the toSignal(toObservable(this.id)) is long and not easy for beginners to understand.




state = computed(() => {
const result = this.#personMovies();
return {
fontSize: this.id() % 2 === 0 ? '1.25rem' : '1.75rem',
rgb: generateRGBCode(),
person: signal(result && result.length > 0 ? result[0] : undefined),
films: signal(result && result.length > 1 ? result.slice(1): []),
searchId: signal(this.id()),
};
});






If the HTTP request is successful, the result array is defined. The person property is a signal and the value is the first element of the array. The films property is a signal that tracks the remaining array elements.




<div class="border">
@if(state().person(); as person) {
<p>Id: {{ id() }} </p>
<p>Name: {{ person.name }}</p>
<p>Height: {{ person.height }}</p>
<p>Mass: {{ person.mass }}</p>
<p>Hair Color: {{ person.hair_color }}</p>
<p>Skin Color: {{ person.skin_color }}</p>
<p>Eye Color: {{ person.eye_color }}</p>
<p>Gender: {{ person.gender }}</p>
} @else {
<p>No info</p>
}

<p style="text-decoration: underline">Movies</p>
@for(film of state().films(); track film) {
<ul style="padding-left: 1rem;">
<li>{{ film }}</li>
</ul>
} @empty {
<p>No movie</p>
}
</div>






The HTML template updates to display person and films from the state computed signal.



Conclusions:




  • Angular team said not to abuse effect

  • We can create signals-in-compute and update the properties when the signal they depend on changes

  • Use toSignal and toObservable to make HTTP requests. toSignal(toObservable(this.id)) is long and hard to read, and we can check the toObservableSignal function in the ngxtension library.










Resources:



Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Use effect less in Angular, try signals in computed

Thematisch verwandte Begriffe: effect, less, Angular, signals · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94036 | A security flaw has been discovered in D-Link DIR-X1860 and DIR-X1860Z u…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick