N
N
No Name2018-06-17 21:53:34
Angular
No Name, 2018-06-17 21:53:34

How to track changes to a variable inside a component?

1. There is an array inside the component;
2. A new element is added to this array in several different places inside the component (in the createMessage() method and inside the getIncomingMessagesStream() subscriber);
3. Every time a new element is added to the array, you need to perform several actions at once (scroll down the page and some more);
4. Instead of writing additional actions in several places, I want to do it at one point inside the component when a new value is added to my array.
How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexaZem, 2018-06-22
@AlexaZem

It is possible to throw out data in general in service which to zaprovaydit in the general module for these components.
Well, in the service itself, if on the example of user_id:

@Injectable()
export class DataService {
  private userIdSubject = new Subject<number>();
  $userId = this.userIdSubject.asObservable();

  setUserId(id: number) {
    this.userIdSubject.next(id);
  }
}

In the components themselves, if you need to change the data:
(dataSvc: DataService )
In the components that should track changes and receive data:
(dataSvc: DataService )
And don't forget to subscribe

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question