Angular实现简单的toDoList以及数据持久化存储

本文展示了如何在Angular应用中创建一个简单的toDoList,并实现数据的实时更新和本地持久化存储。通过使用ngModel双向数据绑定,实现了待办事项的状态变更,同时通过StorageService将数据存储在localStorage中,确保数据在页面刷新后仍然保留。
摘要由CSDN通过智能技术生成

[](()第二例:ToDoList


功能概述: 在第一例的基础上, 我们还可以通过点击checkBox来实时更改数据状态

HTML:

搜索条

<input type=“text” [(ngModel)]=“keyword” (keyup)=“doAdd($event)” />


正在运行

    <input type=“checkbox” [(ngModel)]=“item.status” (change)="changeCheckBox() "/>{{ item.title }}

    <button (click)=“onDelete(i)”>×

    成功执行

      <input type=“checkbox” [(ngModel)]=“item.status” (change)=“changeCheckBox()” />{{ item.title }}

      <button (click)=“onDelete(i)”>×

      CSS:

      h2{

      text-align: center;

      }

      .toDoList{

      margin: 20px auto;

      width: 400px;

      li{

      line-height: 60px;

      }

      }

      TS:

      import { Component, OnInit } from ‘@angular/core’;

      import { StorageService } from ‘…/…/services/storage.service’;

      @Component({

      selector: ‘app-to-do-list’,

      templateUrl: ‘./to-do-list.c 《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 omponent.html’,

      styleUrls: [‘./to-do-list.component.scss’],

      })

      export class ToDoListComponent implements OnInit {

      public toDoData = [];

      public keyword = ‘’;

      public status = false;

      constructor(public storageService: StorageService) {}

      ngOnInit(): void {

      const storageToDoList = this.storageService.get(‘toDoList’);

      if (storageToDoList) {

      this.toDoData = storageToDoList;

      }

      }

      public doAdd(e: any): void {

      if (e.keyCode === 13) {

      if (this.toDoDataHasKeyWord(this.toDoData, this.keyword)) {

      this.toDoData.push({ title: this.keyword, status: this.status });

      this.keyword = ‘’;

      this.storageService.set(‘toDoList’, this.toDoData);

      }

      }

      }

      public onDelete(key: number): void {

      this.toDoData.splice(key, 1);

      this.storageService.set(‘toDoList’, this.toDoData);

      }

      public toDoDataHasKeyWord(toDoData: any[], keyword: any): boolean {

      // 异步问题先不用

      // this.toDoData.forEach((item) => {

      // });

      return !toDoData.some((item) => item.title === keyword);

      // const { length } = toDoData;

      // for (let i = 0; i < length; ++i) {

      // if (toDoData[i].title === keyword) {

      // return false;

      // }

      // }

      // return true;

      // }

      }

      public changeCheckBox(): void {

      this.storageService.set(‘toDoList’, this.toDoData);

      }

      }

      结果截图:

      在这里插入图片描述

      [](()他们的服务和数据持久化


      Service:

      import { Injectable } from ‘@angular/core’;

      @Injectable({

      providedIn: ‘root’,

      })

      export class StorageService {

      constructor() {}

      public set(key: string, value: any): void {

      localStorage.setItem(key, JSON.stringify(value));

      }

      public get(key: string): any {

      return JSON.parse(localStorage.getItem(key));

      }

      public remove(key: string): void {

      localStorage.removeItem(key);

      }

      }

      • 0
        点赞
      • 0
        收藏
        觉得还不错? 一键收藏
      • 0
        评论
      评论
      添加红包

      请填写红包祝福语或标题

      红包个数最小为10个

      红包金额最低5元

      当前余额3.43前往充值 >
      需支付:10.00
      成就一亿技术人!
      领取后你会自动成为博主和红包主的粉丝 规则
      hope_wisdom
      发出的红包
      实付
      使用余额支付
      点击重新获取
      扫码支付
      钱包余额 0

      抵扣说明:

      1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
      2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

      余额充值