I have a simple code in Angular 18 as to understand the change mechanismus: By the timeintervall the signal A() value will changed and triggert the computed W. The W reference will be pass to signal E. In the result, E will be updated within the same div and only if W is visiable.
appponent.ts:
import {Component, computed, signal} from '@angular/core';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterLink, RouterOutlet],
templateUrl: './appponent.html',
styleUrl: './appponent.scss'
})
export class AppComponent {
title = 'AngularTestV18';
public T: boolean=true
public A=signal(0);
public W = computed(this.A);
E=signal(this.W)
private i=1
private t=setInterval(() => {this.A.set((this.i++));
console.log(this.W());
}, 100);
}
and appponent.html
<div>
App-Template!
<br>
<button (click)="T=!T">W visible</button>
<br>
<div>
<div>
Injected W: {{W()}}
Signal E: {{E()}}
@if (T) { Signal E: {{E()}} }
</div>
<div>
@if (T) { Injected W: {{W()}} }
Signal E: {{E()}}
</div>
</div>
</div>
Why is there a differ between the update strategy of signal E as
App-Template! W/E visible Injected W: 337 Signal E: [Computed: 337] Signal E: [Computed: 109] - updated if W visiable Injected W: 337 Signal E: [Computed: 0] - never updated
?
I have a simple code in Angular 18 as to understand the change mechanismus: By the timeintervall the signal A() value will changed and triggert the computed W. The W reference will be pass to signal E. In the result, E will be updated within the same div and only if W is visiable.
appponent.ts:
import {Component, computed, signal} from '@angular/core';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterLink, RouterOutlet],
templateUrl: './appponent.html',
styleUrl: './appponent.scss'
})
export class AppComponent {
title = 'AngularTestV18';
public T: boolean=true
public A=signal(0);
public W = computed(this.A);
E=signal(this.W)
private i=1
private t=setInterval(() => {this.A.set((this.i++));
console.log(this.W());
}, 100);
}
and appponent.html
<div>
App-Template!
<br>
<button (click)="T=!T">W visible</button>
<br>
<div>
<div>
Injected W: {{W()}}
Signal E: {{E()}}
@if (T) { Signal E: {{E()}} }
</div>
<div>
@if (T) { Injected W: {{W()}} }
Signal E: {{E()}}
</div>
</div>
</div>
Why is there a differ between the update strategy of signal E as
App-Template! W/E visible Injected W: 337 Signal E: [Computed: 337] Signal E: [Computed: 109] - updated if W visiable Injected W: 337 Signal E: [Computed: 0] - never updated
?
Share Improve this question edited Nov 16, 2024 at 20:12 Naren Murali 61.1k5 gold badges44 silver badges81 bronze badges asked Nov 16, 2024 at 17:49 JuergenJuergen 351 silver badge7 bronze badges1 Answer
Reset to default 1You should define signal E
as a computed(this.W)
since the computed will trigger to changes of signal W
. In your question E
signal was defined as a signal of a signal (E=signal(this.W)
), which does not work, since signal E
is calculated by W
, so it's a computed
signal.
Even though we remove the signal W
from the DOM/HTML, it does not mean the signal will not trigger the change detection, it will fire for all signals linked to W
.
import { Component, signal, computed } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
@Component({
selector: 'app-root',
standalone: true,
template: `
<div>
App-Template!
<br>
<button (click)="T=!T">W visible</button>
<br>
<div>
<div>
Injected W: {{W()}}
Signal E: {{E()}}
@if (T) { Signal E: {{E()}} }
</div>
<div>
@if (T) { Injected W: {{W()}} }
Signal E: {{E()}}
</div>
</div>
</div>
`,
})
export class App {
title = 'AngularTestV18';
public T: boolean = true;
public A = signal(0);
public W = computed(this.A);
E = computed(this.W);
private i = 1;
private t = setInterval(() => {
this.A.set(this.i++);
console.log(this.W());
}, 100);
}
bootstrapApplication(App);
Stackblitz Demo
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745648268a4638101.html
评论列表(0条)