0

I am going to Todo List by Angular. I use local storage to save todo items but when I refresh the data not shown, although the data saved in a tab application.

This is firebase link: https://gtodomvc.web.app/

This is stackBlitz link: https://stackblitz.com/github/ntgiang4991/todo?file=src%2Fapp%2Fcomponents%2Ftodo-list%2Ftodo-list.component.ts

the data in local storage

This is my code: File localstorage.service.ts

export class LocalStorageService {
  storage: Storage;

  constructor() { 
    this.storage = window.localStorage
  }

  set(key: string, value: string): void{
    this.storage[key] = value;
  }

  get(key: string): string{
    return this.storage[key] || false;
  }

  setObject(key: string, value: any): void{
    if(!value){
      return;
    }
    this.storage[key] = JSON.stringify(value);
  }

  getValue<T>(key: string): T{
    const obj = JSON.parse(this.storage[key] || null);
    return <T>obj || null;
  }
}

File todo.service.ts

private todos: Todo[] = [];
todos$: Observable<Todo[]> = this.displayTodosSubject.asObservable();

fetchFromLocalStorage(){
    this.todos = this.storageService.getValue<Todo[]>(TodoService.TodoStorageKey) || [];
}

File todo-list.component.ts

todos$: Observable<Todo[]>;
ngOnInit(): void {
    this.todos$ = this.todoService.todos$;
}

File todo-list.component.html

<app-todo-item *ngFor="let todo of todos$ | async" [todo]="todo" 
    (changeStatus)="onChangeTodoStatus($event)" 
    (editTodo)="onEditTodo($event)" 
    (deleteTodo)="onDeleteTodo($event)"
>
</app-todo-item>

File app.component.ts

ngOninit(){
    this.todoService.fetchFromLocalStorage();

    this.hasTodo$ = this.todoService.length$.pipe(map(length => length > 0));
    console.log(this.todoService.length$)
}
Giang Giang
  • 31
  • 1
  • 1
  • 6
  • How more detail about 'todo.service.ts' file. How are `displayTodosSubject` and `length$` defined? – ruth Jun 12 '20 at 10:20
  • I upload to stackblitz. Could you check it? This is stackblitz link: https://stackblitz.com/github/ntgiang4991/todo?file=src%2Fapp%2Fcomponents%2Ftodo-list%2Ftodo-list.component.ts – Giang Giang Jun 13 '20 at 04:43
  • private displayTodosSubject: BehaviorSubject = new BehaviorSubject([]); private lengthSubject: BehaviorSubject = new BehaviorSubject(0); length$: Observable = this.lengthSubject.asObservable(); displayTodosSubject is a BehaviorSubject. It has a prototype that is Todo[] and the initial value is []. length$ is a Observable of lengthSubject. It has the initial value is 0. – Giang Giang Jun 13 '20 at 04:59

1 Answers1

0

try to put your code in this https://stackblitz.com/edit/angular-ivy-pphyej?embed=1&file=src/app/app.component.ts

to make it easy to Colleagues to help you

  • I can try upload to firebase: https://gtodomvc.web.app/ Can you help me? I uploaded to stackblitz but it errors the URL. – Giang Giang Jun 13 '20 at 02:17
  • I uploaded it to stackblitz. If you have free time, could you help me? Thank you so much!!! https://stackblitz.com/github/ntgiang4991/todo?file=src%2Fapp%2Fcomponents%2Ftodo-list%2Ftodo-list.component.ts – Giang Giang Jun 13 '20 at 04:40