Subscribe is a method that comes from rxjs library which Angular is using behind the scene.
If you can imagine yourself when subscribing to a news letter and after the subscribing, every time that there is a new news letter, they will send it to your home
All the AJAX calls in Angular is using this library behind the scene and in order to use any of them, you've got to use the method name, e.g get, and then call subscribe on it, because get returns and Observable.
Also, when you're doing this <button click="dosomething()"></button> Angular is using Observable's behind the scene and subscribes you to that source of thing, which in this case is a click event.
Here subscribe is used along with observable to get the latest updates of the observable data.
Angular Service with Observable : In the below code we are creating a Observable as Observable's are usually denoted as suffix $, in the below code userTextObservable$
Subscribing a Observable :
Here we are subscribing to the above Observable and used to get the updated information from observable.
That's it !!
If you can imagine yourself when subscribing to a news letter and after the subscribing, every time that there is a new news letter, they will send it to your home
All the AJAX calls in Angular is using this library behind the scene and in order to use any of them, you've got to use the method name, e.g get, and then call subscribe on it, because get returns and Observable.
Also, when you're doing this <button click="dosomething()"></button> Angular is using Observable's behind the scene and subscribes you to that source of thing, which in this case is a click event.
Here subscribe is used along with observable to get the latest updates of the observable data.
Angular Service with Observable : In the below code we are creating a Observable as Observable's are usually denoted as suffix $, in the below code userTextObservable$
import { Injectable } from '@angular/core';
import { BehaviorSubject } from '../../../node_modules/rxjs';
@Injectable({
providedIn: 'root'
})
export class PreviewService {
private _userTextSubject = new BehaviorSubject(null);
userTextObservable$ = this._userTextSubject.asObservable();
onUserEneters(userText :string){
this._userTextSubject.next(userText);
}
}
Subscribing a Observable :
Here we are subscribing to the above Observable and used to get the updated information from observable.
import { Component, OnInit } from '@angular/core';
import {PreviewService} from '../services/preview-service.service';
@Component({
selector: 'second-component',
templateUrl: './second-component.component.html',
styleUrls: ['./second-component.component.css']
})
export class SecondComponentComponent implements OnInit {
public previewText:string;
constructor(private previewService :PreviewService) { }
ngOnInit() {
this.previewService.userTextObservable$.subscribe(x=>this.previewText=x);
}
}
That's it !!
8 Comments
The site is very interesting. I truly feel this amazing site requirements far more consideration. .This is one wonderful blog article.
ReplyDeletecurrency-converter-free, best-app-currency-converter
This is very interesting, You’re a very skilled blogger. I’ve joined your rss feed and look forward to seeking more of your magnificent post. Also, I’ve shared your website in my social networks!
ReplyDeleteautoketing
sales pop master
sales pop master online
I'm flawed to reveal this page. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info.
ReplyDeletefree game Toon Cup 2017 Break The Cup For boy free game Battle for the Galaxy
Outstanding blog with lots of information. Keep posting more like this.
ReplyDeleteAngularJS Training in Chennai
AngularJS course in Chennai
Angular 6 Training in Chennai
ReactJS Training in Chennai
Web Designing course in Chennai
Salesforce Training in Chennai
ccna course in Chennai
Ethical Hacking course in Chennai
AngularJS Training in Velachery
The cultural history of Inter-war Europe is filled with such powerful and influential interpretations of a reality that defeats the human capacity to imagine evil. Take Picasso's Guernica for instance. In June 1937, Picasso finished painting the Modernist classic in response to another April tragedy: the bombing of the Spanish city of Guernica during the civil war. It was his anguished creativity reacting to George Steer's eyewitness account of the incident.
ReplyDeletekizi Games free
Jogos 2019
Games friv online
Good day. I was impressed with your article. Keep it up . You can also visit my site if you have time. Thank you and Bless you always.
ReplyDeletebrc certification malaysia
Thanks for sharing angular training in chennai
ReplyDeleteGreat job for publishing such a nice article. Your article isn’t only useful but it is additionally really informative. Thank you because you have been willing to share information with us. Neo Tv Pro IPTV Subscription Online
ReplyDelete