小编Sou*_*bii的帖子

Angular 2 - 从localStorage存储和获取对象数组

我想在localStorage中存储一组对象.

这是我的代码片段,用于存储组件阶段的对象数组.

this._authenticationService.getProfilByLogin(this.form.value.j_username)
  .subscribe(result => {
     console.log('inside getting profils');
     console.log(result.json().ecomServices);
     localStorage.setItem('services_assigned', result.json().ecomServices);
  }, error => {
Run Code Online (Sandbox Code Playgroud)

这是试图将其恢复到另一个组件中的代码.

import {Component, OnInit} from '@angular/core';
  import {EcomService} from "../../model/EcomService";

  @Component({
    selector: 'app-sidebar-nav',
    templateUrl: './sidebar-nav.component.html',
    styleUrls: ['./sidebar-nav.component.css']
  })
  export class SidebarNavComponent implements OnInit {


    public ecomServices: EcomService[] = [];

    constructor() {
    }

    ngOnInit() {
      this.ecomServices = localStorage.getItem('services_assigned');
    }
  }
Run Code Online (Sandbox Code Playgroud)

这是我的模特

export class EcomService {

  public eseCode: number;
  public eseLabel: string;

}
Run Code Online (Sandbox Code Playgroud)

angular-local-storage angular

2
推荐指数
1
解决办法
9287
查看次数

标签 统计

angular ×1

angular-local-storage ×1