Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make angular effect just execute once

Tags:

angular

I have an angular effect which I only want to be executed once.

But because it depends on other signals, I want it to wait first until all data is present. After that first execution, I don't want it to be triggered again.

I am looking for an elegant solution (not just some boolean to flip)

With Observables you could effectively use takeUntil or take(1). But what is the signal equivalent to this?

like image 419
bvdb Avatar asked May 18 '26 12:05

bvdb


1 Answers

As mentioned in the angular doc:

Effects return an EffectRef that can be used to destroy them manually, via the .destroy() operation. This can also be combined with the manualCleanup option to create an effect that lasts until it is manually destroyed.

const effectRef =  effect(()=>{
    if(this.a() && this.b()){
      console.log('ReadValue');
      effectRef.destroy(); 
    }
},{manualCleanup:true});

Example

like image 143
Chellappan வ Avatar answered May 20 '26 00:05

Chellappan வ