Skip to content

Commit d51d714

Browse files
rxjs inbtroduction
1 parent ace3726 commit d51d714

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

Angular-Demo/ecomApp/src/app/product/product.component.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Component,
55
DoCheck,
66
ElementRef,
7+
OnDestroy,
78
OnInit,
89
Optional,
910
QueryList,
@@ -12,9 +13,11 @@ import {
1213
ViewChildren,
1314
ViewEncapsulation,
1415
} from '@angular/core';
16+
import { Subscription } from 'rxjs';
1517
import { HeaderComponent } from '../header/header.component';
1618
import { LoggerService } from '../logger/logger.service';
1719
import { Product } from './product';
20+
import { ProductServiceRxJs } from './rxjx-demo/rxjs-demo';
1821
import { ProductService } from './services/product.service';
1922

2023
@Component({
@@ -29,7 +32,7 @@ import { ProductService } from './services/product.service';
2932
// providers: [ProductService],
3033
})
3134
export class ProductComponent
32-
implements OnInit, DoCheck, AfterViewInit, AfterViewChecked
35+
implements OnInit, DoCheck, AfterViewInit, AfterViewChecked, OnDestroy
3336
{
3437
title = 'Product Information';
3538

@@ -49,16 +52,17 @@ export class ProductComponent
4952

5053
currentValue = '';
5154

52-
5355
@ViewChild(HeaderComponent)
5456
headerComponent!: HeaderComponent;
5557

5658
@ViewChildren(HeaderComponent)
5759
headerChildrenComponent!: QueryList<HeaderComponent>;
5860

61+
subscription!: Subscription;
62+
5963
@ViewChild('apiError', { static: true }) errorDiv!: ElementRef<any>;
6064
constructor(
61-
private productService: ProductService,
65+
private productService: ProductServiceRxJs,
6266
@Optional() private logService: LoggerService
6367
) {}
6468

@@ -67,7 +71,10 @@ export class ProductComponent
6771
// this.logService? this.logService.log('test')
6872
console.log(this.headerComponent);
6973
console.log(this.errorDiv);
70-
this.productList = this.productService.getProducts();
74+
// this.productList =
75+
this.subscription = this.productService
76+
.getProducts()
77+
.subscribe((data) => console.log(data));
7178
}
7279

7380
ngDoCheck() {
@@ -120,4 +127,8 @@ export class ProductComponent
120127
deleteProduct(product: Product) {
121128
this.selectedProduct = product;
122129
}
130+
131+
ngOnDestroy() {
132+
this.subscription?.unsubscribe();
133+
}
123134
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Injectable } from '@angular/core';
2+
import { of } from 'rxjs';
3+
import { tap } from 'rxjs/operators';
4+
import { Product } from '../product';
5+
6+
@Injectable({
7+
providedIn: 'root',
8+
})
9+
export class ProductServiceRxJs {
10+
products: Product[] = [
11+
{ id: 1, name: 'Iphone X', mfd: new Date('1-Jan-2021'), price: 50000 },
12+
{ id: 2, name: 'one plus 8', mfd: new Date('1-Jan-2019'), price: 40000 },
13+
{ id: 3, name: 'Samsung', mfd: new Date('1-Jan-2018'), price: 30000 },
14+
];
15+
16+
getProducts() {
17+
return of<Product[]>(this.products);
18+
}
19+
}

0 commit comments

Comments
 (0)