daily pastebin goal
16%
SHARE
TWEET

Untitled

a guest Nov 22nd, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import { HomeService } from './service/home.service';
  3.  
  4. @Component({
  5.   selector: 'app-home',
  6.   templateUrl: './home.component.html',
  7.   styleUrls: ['./home.component.scss']
  8. })
  9. export class HomeComponent implements OnInit {
  10.  
  11.   itemCount: number = 0;
  12.   todo = [];
  13.   addingVar: Object;
  14.   dueDate: string;
  15.   constructor(private homeService: HomeService) { }
  16.  
  17.   ngOnInit() {
  18.     let ltodo = []; // ADDED THIS TO FORCE VALUE STORAGE
  19.     this.homeService.getToDo().subscribe(
  20.       function(response) {
  21.         ltodo = response;
  22.       },
  23.       function(error) { console.log('Error happened', error); },
  24.       function() { }
  25.     );
  26.     setTimeout(function(){ // <-HACK TO FORCE IT TO STORE DATA AT LEAST WITHIN "ngOnInit"
  27.                            // I'LL USE A DEBOUNCED FUNCTION INSTEAD LATER
  28.       this.todo = ltodo;
  29.       this.itemCount = this.todo.length;
  30.       console.log(this.todo);      // <- (array of 9 objects) works!!
  31.       console.log(this.itemCount); // <- 9                    works!!
  32.     }, 10);
  33.   }
  34.  
  35.   addItem() {
  36.     console.log(this.todo); // <- this.todo IS NOW EMPTY ARRAY! WHY??
  37.     this.addingVar = {name: this.todoTitle, description: this.todoText, date: Date.now(), dueDate: this.dueDate };
  38.     console.log(this.addingVar);
  39.     this.homeService.postToDo(this.addingVar).subscribe(
  40.       function(response) { console.log('Success Response', response); },
  41.       function(error) { console.log('Error happened', error); },
  42.       function() { }
  43.     );
  44.   }
  45. }
  46.    
  47. import { Injectable } from '@angular/core';
  48. import { Http, Response } from '@angular/http';
  49. import 'rxjs/add/operator/map';
  50.  
  51. @Injectable()
  52. export class HomeService {
  53.   constructor (
  54.     private http: Http
  55.   ) {}
  56.   getToDo() {
  57.       return this.http.get(`http://localhost:3000/todos`)
  58.         .map((res: Response) => res.json());
  59.   }
  60.   getToDoId(id: string) {
  61.     return this.http.get(`http://localhost:3000/todos/${id}`)
  62.       .map((res: Response) => res.json());
  63.   }
  64.   postToDo(body: object) {
  65.     return this.http.post(`http://localhost:3000/todos`, body)
  66.       .map((res: Response) => res.json());
  67.   }
  68.   putToDoId(id: string, body: object) {
  69.     return this.http.put(`http://localhost:3000/todos/${id}`, body)
  70.       .map((res: Response) => res.json());
  71.   }
  72.   deleteToDoId(id: string) {
  73.     return this.http.delete(`http://localhost:3000/todos/${id}`)
  74.       .map((res: Response) => res.json());
  75.   }
  76. }
RAW Paste Data
Top