Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

angular unsubscribe from observable

//Please note that there are many ways to unsubscribe, this is one of them
import { Subscription } from 'rxjs';

private searchEventSubscription: Subscription;

export class someComponent OnInit, OnDestroy {

    constructor(private someService: SomeService) { }

    ngOnInit() {
      this.searchEventSubscription = this.someService.someMethod.subscribe(result => {  
        doSomething(result);
      });
    }

    ngOnDestroy() {
		this.searchEventSubscription.unsubscribe()
    }
Comment

angular subscribe and unsubscribe

import { Component, OnDestroy, OnInit } from '@angular/core';
// RxJs 6.x+ import paths
import { filter, startWith, takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
import { BookService } from '../books.service';

@Component({
    selector: 'app-books',
    templateUrl: './books.component.html'
})
export class BooksComponent implements OnDestroy, OnInit {
    private ngUnsubscribe = new Subject();

    constructor(private booksService: BookService) { }

    ngOnInit() {
        this.booksService.getBooks()
            .pipe(
               startWith([]),
               filter(books => books.length > 0),
               takeUntil(this.ngUnsubscribe)
            )
            .subscribe(books => console.log(books));

        this.booksService.getArchivedBooks()
            .pipe(takeUntil(this.ngUnsubscribe))
            .subscribe(archivedBooks => console.log(archivedBooks));
    }

    ngOnDestroy() {
        this.ngUnsubscribe.next();
        this.ngUnsubscribe.complete();
    }
}
Comment

how to unsubscribe from observable created by http

import { Component, OnInit, OnDestroy } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

interface User {
  id: string;
  name: string;
  age: number;
}

@Component({
  selector: 'app-foobar',
  templateUrl: './foobar.component.html',
  styleUrls: ['./foobar.component.scss'],
})
export class FoobarComponent implements OnInit, OnDestroy {
  private user: User = null;
  private destroy$ = new Subject();

  constructor(private http: HttpClient) {}

  ngOnInit() {
    this.http
      .get<User>('api/user/id')
      .pipe(takeUntil(this.destroy$))
      .subscribe(user => {
        this.user = user;
      });
  }

  ngOnDestroy(): void {
    this.destroy$.next();  // trigger the unsubscribe
    this.destroy$.complete(); // finalize & clean up the subject stream
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: mat dialog disable close 
Typescript :: is assigned a value but never used 
Typescript :: Could not find method kapt() for arguments [androidx.room:room-compiler:2.3.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. 
Typescript :: vue router get full string query 
Typescript :: circle dot in latex 
Typescript :: js check if function is promise 
Typescript :: upload file requests python 
Typescript :: typescript array with allowed object keys 
Typescript :: how to run ts file 
Typescript :: three dots dropdown menu bootstrap 
Typescript :: typescript method comments 
Typescript :: There can only be one default row without a when predicate function. 
Typescript :: nested array typescript 
Typescript :: gradients colors in android 
Typescript :: typescript moment type 
Typescript :: withStyles(DateRangePicker) 
Typescript :: create an array for looping typescript 
Typescript :: reported error code “128” when it ended: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. 
Typescript :: check runnong ports ubuntu 
Typescript :: wordpress get_posts custom posts by meta key 
Typescript :: geodataframe from lat lon points python 
Typescript :: How to Solve Property ‘getContext’ does not exist on type ‘HTMLElement’ error in Angular 12 & TypeScript 
Typescript :: deno current directory 
Typescript :: best way to round to two typescript 
Typescript :: typescript array of object with types 
Typescript :: adding two lists using lambda function 
Typescript :: get random dark color 
Typescript :: ganache 
Typescript :: admin_enqueue_scripts specific page 
Typescript :: python get list elements missing in one list 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =