How does the "next" function work in Angular?

Asked

Viewed 558 times

0

Follow the example:

@Output() exemplo = new Subject<{ valor1: number, valor2: number }>();

this.exemplo.next({ valor1: _valor1, valor2: this.valor });
  • What’s going on here?
  • An object is passed as a parameter?
  • I actually believe this has more to do with Generators than with Angular itself

  • Only with this code can not affirm anything, the method next() in the Angular can be applied in several contexts, it can be executed from an Observable, or in a Subject for example, this already explains something, as both are "observing" changes in some declared value.

  • I added a line of code to the example. It looks like you have Subject.

1 answer

1


By instantiating a Subject it is necessary to state what kind of data will work, for example: new Subject<number>();. In your example the type of object that the Subject will work is an object with the following structure: { valor1: number, valor2: number }, i.e., the object must have two fields of the type number with the names "valor1" and "valor2".

When using the function next you issue a value to subscribers, in other simpler terms, you publish a value to those listening.

See this small example taken from documentation:

import { Subject } from 'rxjs';

const subject = new Subject<number>();

subject.subscribe({
  next: (v) => console.log(`observerA: ${v}`)
});
subject.subscribe({
  next: (v) => console.log(`observerB: ${v}`)
});

subject.next(1);
subject.next(2);

// Logs:
// observerA: 1
// observerB: 1
// observerA: 2
// observerB: 2

If you want to dig deeper:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.