C0mpl3x
C0mpl3x

Reputation: 532

How to trigger event when array item value changes

I have objects coming in to my array. When change the value of the objects, let's say id. I need an event or something like that to be triggered.

This is how I add my objects:

 const entry= new CompletableEntry('models.Address.addressLine2._TITLE');
 this.globalLoadingScreenService.addTask(entry);
 task.updateID(3);

Updating the ID won't trigger anything but I have a function that needs to be triggered when a value changes. The function checks if all the objects have status of complete and if yes it will delete all items in array.

I know one way to get this would be by adding them trough my service but I kind of require solution that won't update the task status trough service.

Upvotes: 0

Views: 1887

Answers (1)

Nischaya Sharma
Nischaya Sharma

Reputation: 471

You can either create a custom observable array to which you can subscribe to. This might help.

What I came up with is you can create a BehaviourSubject and subscribe to it.

import { BehaviorSubject } from 'rxjs';
arrayChanged = new BehaviourSubject(false);

//The function where actually the array is updating
fun update() {
    ...
    this.arrayChanged.next(!this.arrayChanged.value);
}

// Then to listen to the array changes.

this.arrayChanged.subscribe( value => {
    //The function you want to run goes here.
    // this block runs everytime there is some change in the behaviour subject
})

Upvotes: 2

Related Questions