我想在刷新页面后禁用该按钮

问题描述 投票:0回答:2

我有一系列名为人物的对象。我试图禁用按钮应该保持禁用,即使刷新页面后。请帮忙

    <div *ngFor="let item of people">
      <button [disabled]="item.disabled"(click)="item.disabled=true">Hi</button> 
    </div>
angular angular6
2个回答
1
投票

刷新页面类似于重新启动单页Web应用程序。刷新时,默认情况下会重置组件和对象状态。如果您希望对象状态在刷新后继续存在,您只需要通过本地或会话存储来存储它,或者从OnInit的外部服务获取它


0
投票

尝试使用localStorage

HTML代码:

<div *ngFor="let item of people">
  <button  mat-stroked-button color="primary" [disabled]="item.disabled" (click)="disableItem(item)">Hi</button>      
</div>

<button mat-stroked-button color="primary" (click)="reset()">Reset</button>

TS代码:

import { Component } from '@angular/core';

export interface Section {
  name: string;
  updated: Date;
}

/**
 * @title List with sections
 */
@Component({
  selector: 'list-sections-example',
  styleUrls: ['list-sections-example.css'],
  templateUrl: 'list-sections-example.html',
})
export class ListSectionsExample {
  people: any[] = [
    {
      name: 'Photos',
      updated: new Date('1/1/16'),
    },
    {
      name: 'Recipes',
      updated: new Date('1/17/16'),
    }
  ];

  constructor() {
    this.people = JSON.parse(localStorage.getItem('people'));
  }

  reset() {
    this.people = [
      {
        name: 'Photos',
        updated: new Date('1/1/16'),
      },
      {
        name: 'Recipes',
        updated: new Date('1/17/16'),
      }
    ];
    localStorage.setItem('people', JSON.stringify(this.people))
  }

  disableItem(data) {
    data.disabled = true;
    localStorage.setItem('people', JSON.stringify(this.people))
  }
}

Working Demo

© www.soinside.com 2019 - 2024. All rights reserved.