一、history搜索框

1-1.history.component.html

  1. <input type="text" (keyup)="handleInput($event)" [(ngModel)]="msg">
  2. <ul>
  3. <li *ngFor="let item of lists;let key=index" (click)="handleClick(key)">{{item}}</li>
  4. </ul>

1-2.history.component.ts

  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'app-history',
  4. templateUrl: './history.component.html',
  5. styleUrls: ['./history.component.css']
  6. })
  7. export class HistoryComponent implements OnInit {
  8. msg = "";
  9. lists: string[] = [];
  10. constructor() { }
  11. ngOnInit(): void {
  12. if (localStorage.getItem("lists")) {
  13. let lists = JSON.parse(<string>localStorage.getItem('lists'));
  14. this.lists = lists
  15. }
  16. }
  17. handleInput(e: any) {
  18. if (e.keyCode == 13) {
  19. this.lists.push(this.msg);
  20. this.msg = "";
  21. localStorage.setItem("lists", JSON.stringify(this.lists));
  22. }
  23. }
  24. handleClick(key: number) {
  25. this.lists.splice(key, 1);
  26. localStorage.setItem("lists", JSON.stringify(this.lists));
  27. }
  28. }

二、todoList

2-1.todo-list.component.html

  1. <input type="text" [(ngModel)]="msg" (keyup)="handleInput($event)">
  2. <p>未完成 {{todo.length}}</p>
  3. <div *ngFor="let item of todo;let key=index">
  4. <input type="checkbox" [checked]="item.checked" (click)="handleClick(item,key)">
  5. {{item.value}}
  6. </div>
  7. <p>已完成 {{complete.length}}</p>
  8. <div *ngFor="let item of complete;let key=index">
  9. <input type="checkbox" [checked]="item.checked" (click)="handleClick(item,key)">
  10. {{item.value}}
  11. </div>

2-2.todo-list.component.ts

  1. import { Component, OnInit } from '@angular/core';
  2. type data = { value: string, checked: Boolean };
  3. @Component({
  4. selector: 'app-todo-list',
  5. templateUrl: './todo-list.component.html',
  6. styleUrls: ['./todo-list.component.css']
  7. })
  8. export class TodoListComponent implements OnInit {
  9. msg = "";
  10. todo: data[] = [];
  11. complete: data[] = [];
  12. constructor() { }
  13. ngOnInit(): void {
  14. if (localStorage.getItem("todo")) {
  15. this.todo = JSON.parse(<string>localStorage.getItem("todo"))
  16. };
  17. if (localStorage.getItem("complete")) {
  18. this.complete = JSON.parse(<string>localStorage.getItem("complete"))
  19. }
  20. }
  21. handleInput(e: any) {
  22. if (e.keyCode == 13) {
  23. this.todo.unshift({
  24. value: this.msg,
  25. checked: false,
  26. });
  27. this.msg = "";
  28. localStorage.setItem("todo", JSON.stringify(this.todo))
  29. }
  30. }
  31. handleClick(item: data, key: number) {
  32. item.checked = !item.checked
  33. if (item.checked) {
  34. this.todo.splice(key, 1);
  35. this.complete.unshift(item)
  36. } else {
  37. this.complete.splice(key, 1);
  38. this.todo.unshift(item);
  39. }
  40. localStorage.setItem("todo", JSON.stringify(this.todo));
  41. localStorage.setItem("complete", JSON.stringify(this.complete));
  42. }
  43. }

三、Tips报错解决

  1. 1ngModel双向数据绑定需要在app.module.ts中导入FormsModule模块
  2. 2、不能直接定义todo:object=[];这样每个item中的属性也会为object,渲染For循环item.value时会报错。
  3. 使用自定义类型
  4. type data = { value: string, checked: Boolean };
  5. 定义对象中的每个属性类型
  6. 3、使用JSON.parse(localStorage.getItem())时不能直接将string|null类型的数据赋值给string
  7. if判断localStorage存在后直接断言string类型
  8. JSON.parse(<string>localStorage.getItem("complete"))