Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. export const ShoppingList = {
  2. id: 22,
  3. username: 'Duff',
  4. groceries: [
  5. {
  6. day: 'Monday',
  7. purchases: [ { item: 'Beer', price: 10 }, { item: 'Pizza', price: 15 } ]
  8. },
  9. {
  10. day: 'Tuesday',
  11. purchases: [ { item: 'Wine', price: 10 }, { item: 'Steak', price: 15 } ]
  12. },
  13. ]
  14. }
  15.  
  16. import { Injectable } from '@angular/core';
  17. import { ShoppingList } from './data.js'
  18. //
  19. import { BehaviorSubject } from 'rxjs';
  20. import { pluck } from 'rxjs/operators';
  21.  
  22. @Injectable({
  23. providedIn: 'root'
  24. })
  25.  
  26. export class DataService {
  27. shopping_list$ = new BehaviorSubject<any>(ShoppingList);
  28.  
  29. user_name$ = this.shopping_list$.pipe(pluck('username');
  30. id$ = this.shopping_list$.pipe(pluck('id')
  31.  
  32. **day$ = new BehaviorSubject<[]>(ShoppingList_groceries[0].day);**
  33.  
  34. **purchases$ = new BehaviorSubject<[]>(ShoppingList_groceries[0].purchases);**
  35.  
  36. constructor() {}
  37. }
  38.  
  39. import { DataService } from '../service/data.service.ts';
  40. //
  41. import { from, Observable } from 'rxjs';
  42. import { tap, map, pluck, reduce, switchMap } from 'rxjs/operators';
  43.  
  44. @Component({
  45. selector: 'app-list',
  46. templateUrl: './list.component.html',
  47. styleUrls: ['./list.component.css']
  48. })
  49.  
  50. export class ListComponent implements OnInit {
  51.  
  52. shopping_list = this.dataService.shopping_list$;
  53. user_name = this.dataService.user_name$;
  54. id = this.dataService.id$;
  55. day = this.dataService.day$
  56.  
  57. ***prices = this.dataService.purchases$.pipe(
  58. pluck('prices'),
  59. reduce((a:number, c:number) => a + c),
  60. tap(y => console.log(y))
  61. );***
  62.  
  63.  
  64. constructor(private dataService: DataService) {}
  65.  
  66.  
  67.  
  68. ngOnInit() {
  69. this.shopping_list.subscribe(val => console.log(val))
  70. this.prices.subscribe(val => console.log(val))
  71. }
  72.  
  73. }
  74.  
  75. <h1>{{ user_name }} {{ id }} </h1>
  76. <p>{{ day }}</p>
  77. <p>{{ prices }}</p>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement