Advertisement
Shell_Casing

component

Dec 16th, 2018
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {Component, OnDestroy, OnInit} from '@angular/core';
  2. import {Observable, Subscription} from 'rxjs';
  3.  
  4. import { ArticleInterface } from '../../article-interface';
  5. import { ArticleService } from '../../article.service';
  6.  
  7. @Component({
  8.   selector: 'app-list',
  9.   templateUrl: './list.component.html',
  10.   styleUrls: ['./list.component.css']
  11. })
  12. export class ListComponent implements OnInit, OnDestroy {
  13.  
  14.   subscriptions: Subscription[];
  15.   filteredArticles: ArticleInterface[];
  16.  
  17.   constructor(private articleService: ArticleService) { }
  18.  
  19.   ngOnInit() {
  20.     this.getArticles();
  21.   }
  22.  
  23.   getArticles() {
  24.     this.articleService.getAllArticles().subscribe((articles: ArticleInterface[]) => {
  25.       console.log(articles);
  26.       this.articleService.articles = articles;
  27.       this.articleService.articles = this.articleService.articles.reverse();
  28.       this.filteredArticles = this.articleService.articles;
  29.     });
  30.   }
  31.  
  32.  
  33.   filterArticles(query) {
  34.     this.filteredArticles = query ?
  35.       this.articleService.articles.filter(article => article.title.toLowerCase().includes(query.toLowerCase())) :
  36.       this.articleService.articles;
  37.   }
  38.  
  39.   ngOnDestroy() {
  40.     this.subscriptions.map( s => s.unsubscribe());
  41.   }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement