【问题标题】:Can't retrieve data from local storage (Angular 8)无法从本地存储中检索数据(Angular 8)
【发布时间】:2019-11-26 04:27:00
【问题描述】:

现在我希望在用户单击输入字段时进行验证,因为它已经显示条件“请填写”

> import { Component, OnInit, Input } from '@angular/core'; import {
> Router } from '@angular/router'; import { FormBuilder, FormGroup,
> FormArray } from '@angular/forms'; import { Validators } from
> '@angular/forms';
> 
> 
> 
> @Component({   selector: 'todo-app',   templateUrl:
> './todo-app.component.html',   styleUrls: ['./todo-app.component.css']
> }) export class TodoAppComponent implements OnInit {
> 
>    myForm: FormGroup; todoitems = []; todolist; submitted = false;
> 
> 
>   constructor(private router:Router, private formBuilder: FormBuilder
> ) {    }
>   
> 
>   ngOnInit() {
>     this.myForm = this.formBuilder.group({
>       'todoitems': [this.todolist,Validators.compose([Validators.required,Validators.minLength(3)])]
>       
>     })
> 
>     this.todoitems = JSON.parse(localStorage.getItem('todoitems'));   }
> 
>   get todoForms() {
>     return this.myForm.get('todoitems') as FormArray   }
> 
>   addTodo(todolist) {
>     
>     if(this.myForm.invalid){
>       return;
>     }
>     if (this.todoitems) {
>       this.todoitems.push(todolist);
>       if (this.myForm.valid) {
>         console.log("Form Submitted!");
>         this.myForm.reset();
>       }
>       localStorage.setItem('todoitems', JSON.stringify(this.todoitems));
>     }   }
>   
> 
>   deletetodo(index){
>       this.todoitems.splice(index, 1); 
>       localStorage.setItem('todoitems', JSON.stringify(this.todoitems));
>        
>       
>   
> 
>   
>       } get f() { return this.myForm.controls; }
> 
> }
<

form [formGroup]="myForm" >
        
        <div class="input">
                
            <input formControlName="todoitems"  [(ngModel)]="todolist" name="todoitems"  >
            <div *ngIf="myForm.controls.todoitems.errors">
                    <div *ngIf="myForm.controls.todoitems.errors.required"> please fill</div>
                    <div *ngIf="myForm.controls.todoitems.errors.minlength">min 3</div>
                    <div *ngIf="submitted && f.todoitems.errors.apply(this.submitted)">
                        <div *ngIf="f.todoitems.errors.required">First Name is required</div>
                  
            </div>
            </div>
       

               <button  type="submit" (click)="addTodo(todolist)">Add</button >
            <table style="width:50%">
            <tr *ngFor="let todoitem of todoitems; let i = index">
             <td>{{ todoitem }}</td>

             <td>
                    <button (click)="deletetodo(i)">Delete</button>
             </td>
            </tr>
            
           
        
                
         </table>
        </div>
       


      
            
    </form>

【问题讨论】:

  • 您想从哪里获得这些物品?喜欢localStorage.getItem('todoitems')
  • localStorage.setItem('todoitems', JSON.stringify(this.todoitems));设置数据到本地存储后,需要从本地存储localStorage.getItem('todoitems');获取数据

标签: node.js angular angular-local-storage


【解决方案1】:

你可以这样试试:

https://www.npmjs.com/package/ngx-webstorage-service

import { SESSION_STORAGE, StorageService } from 'ngx-webstorage-service';

在您导入存储对象的 ts 文件中:

【讨论】:

  • @MalikShafi 检查您的待办事项。
  • 它使用相同的代码工作正常,只是在检索数据时出现问题。但是使用 webstorage 模块甚至无法将数据存储到本地存储中
  • @MalikShafi,它将检查用于存储值的键/值格式。我认为您是直接在一个键中添加一个数组。
【解决方案2】:

您将数据存储在 localStorage 中,但您永远不会检索存储的数据。因此,要检索数据,您可以使用localStorage.getItem('key')

(此方案没有任何第三方库)

this.todoitems = JSON.parse(localStorage.getItem('todoitems'));

TSngOnInit()代码:

ngOnInit() {
  this.myForm = this.formBuilder.group({
    'todoitem': this.todolist
  })

  this.todoitems = JSON.parse(localStorage.getItem('todoitems')); -- this line
} 

Working-Demo

【讨论】:

  • 刚刚发现一个新错误,如果我从浏览器中删除本地存储文件,它会给出错误“无法读取属性‘push’ of null”
  • 同样的错误兄弟,甚至尝试通过复制粘贴的方式编写代码,但添加功能不起作用
  • 您可以与 AnyDesk 共享屏幕吗?
  • 现在不能,我会自己弄清楚,否则我会联系你。再次感谢兄弟
  • @MalikShafi 你知道了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-26
相关资源
最近更新 更多