Are Angular Signals safe for use in the template? – Angular

by
Ali Hasan
angular angular-signals aws-step-functions function-templates

Quick Fix: Use template diagnostics to ensure signals are executed and follow best practices for using functions in Angular templates.

The Problem:

It is known to avoid function calls in Angular templates for performance reasons. However, it is unclear whether signals fall under this restriction. Can signals be directly invoked within templates without significant performance implications?

The Solutions:

Solution 1: Use signals in the template whenever required

Previously, calling methods in the template was considered an antipattern as it might hide some costly calls. However, this is not the case with signals. Signals don’t have any values until they are called in a template. In fact, there’s even a PR to provide a template diagnostic for when signals aren’t being executed.

Therefore, it is perfectly acceptable to call signals directly in the template. They were specifically designed for that purpose.

Q&A

Are signals handled in a special way inside Angular templates?

Yes, they are intended to be called directly in templates and have no return value until they are called.

What are the downsides of calling methods in Angular templates?

It can be difficult to reason about performance and can lead to unexpected behavior.

Video Explanation:

The following video, titled "I've been using the new Angular control flow syntax for templates (it's ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

My modern Angular course: https://angularstart.com/ We take a first look at the new @ syntax coming in Angular 17 and how it compares to the ...