Doesn't the data act as an observer here?
No, in this scenario, the data
represents the emitted value.
When subscribing to an observable, you can provide handlers for next
, error
, and complete
.
For example:
someObservable$.subscribe(
next => console.log(`next value is: ${next}`),
err => console.log(`error is: ${err}`),
() => console.log('someObservable$ completed!)
);
In your case, only the first handler is provided in the subscribe()
method. You can access the emitted value using the parameter name data
:
this.authservice.registerform(form).subscribe(
data => {
// 'data' refers to the emitted value
this.ifsuccess = true;
this.timer = setInterval(() => {
this.router.navigate(['']);
}, 1200);
}
);
If you need to handle completion or error, you can provide the other handlers as well:
this.authservice.registerform(form).subscribe(
data => {
// 'data' refers to the emitted value
this.ifsuccess = true;
this.timer = setInterval(() => {
this.router.navigate(['']);
}, 1200);
},
error => console.log('error occurred!'),
() => console.log('registerform call completed')
);
Alternatively, you can pass a complete observer object to the subscribe()
method instead of individual handler methods:
const myObserver = {
next: val => console.log(`next val: ${next}`),
error: error => console.log(`error: ${error}`),
complete: () => console.log('complete!')
};
someObserver$.subscribe(myObserver);
Here's a StackBlitz link with a sample demonstration.