Ajai Ramalingam
Ajai Ramalingam

Reputation: 11

Angular 18 input signal instead of @Input

Hi I'm having doubt I'm using input signal instead of @Input I'm sending data from parent component one as simple variable like and another as signal

name = "ajai" name=signal("ajai")

Both are working what is the difference between these two statement. I thought input() is a signal it accepts only signal but it accepting normal string and effect also got triggered How ?

I would like to know which is best way to use name = "ajai" or name=signal("ajai") and when to use name = "ajai" or name=signal("ajai").

Upvotes: 1

Views: 186

Answers (1)

Naren Murali
Naren Murali

Reputation: 57986

Signals:

name=signal("ajai") 
fullName = computed(() => (`Grand Master ${this.name()}`);

In the above example, the updates to name notify the consumers of name like fullName which is a state derived from the original state name.

So signals have the advantage that they are reactive, the changes to the actual values, trigger automatic updates on other reactive elements (computed, effect, resource, rxResource, linkedSignal) and make your code update only when it is actually needed and not all the time.

For best use of signals. Consider enable ChangeDetectionStrategy.OnPush so that change detection is not firing multiple times.


Normal Properties:

name = "ajai"

They can store values, but do not notify other blocks that use these variables for computations.

They are updated on each change detection cycle, note that updating these properties do not trigger change detection, but angular pre signals, fires change detection multiple times during an application lifecycle.

Upvotes: 0

Related Questions