Skip to main content

Subscribe() in Angular

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$


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 !!

Comments

  1. The site is very interesting. I truly feel this amazing site requirements far more consideration. .This is one wonderful blog article.
    currency-converter-free, best-app-currency-converter

    ReplyDelete
  2. 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!
    autoketing
    sales pop master
    sales pop master online

    ReplyDelete
  3. 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.
    free game Toon Cup 2017 Break The Cup For boy free game Battle for the Galaxy

    ReplyDelete
  4. 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.
    kizi Games free
    Jogos 2019
    Games friv online

    ReplyDelete
  5. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete
  6. Thank for this blog are more informative step by step and useful information. I here by attached my site would you see this blog.

    7 tips to start a career in digital marketing

    “Digital marketing is the marketing of product or service using digital technologies, mainly on the Internet, but also including mobile phones, display advertising, and any other digital medium”. This is the definition that you would get when you search for the term “Digital marketing” in google. Let’s give out a simpler explanation by saying, “the form of marketing, using the internet and technologies like phones, computer etc”.

    we have offered to the advanced syllabus course digital marketing for available join now

    more details click the link now

    https://www.webdschool.com/web-development-course-in-chennai.html

    ReplyDelete

Post a Comment