简体   繁体   English

如果键存在于离子存储中,如何使用 * ngIf 显示按钮

[英]How use * ngIf to display a button if the key exists in ionic storage

I working on a App that pulls simple online, json below.我正在开发一个简单的在线应用程序,下面是 json。

json json

this.itemList = { 
  "list": [
    {
      title: "Power Audit",
      text: "666",
      textTwo: "",
      Type: "auditform-main",
      link: this.linkOne
    },
    {
      title: "FLHA Three",
      text: "999",
      textTwo: "",
      Type: "form-main",
      link: this.linkFive
    }
  ]
};

and then an ion-card displays the the data.然后离子卡显示数据。

卡片图像

Here is the html.这是html。

<ion-card  *ngFor="let data of itemList.list let i = index">
  <ion-card-content>
  <!-- saves data to ionic storage -->
  <div class = "linkDB" (click)="saveData(data.link)">
    Download {{data.title}}
  </div>

  <!--<div *ngIf="this.checkForKey(data.title) is true"> ****** Breaks App ****** -->
  <div>
    <!-- checks to see if storage key ionic storage exists -->
    <div class = "dbButton" (click)="checkForKey(data.title)">
      Check For storage key {{data.title}}
    </div>

    <div class = "dbButton" (click)="deleteKeyValue(data.title)" >
      Delete from storage {{data.title}}
    </div>
  </div>
</ion-card-content>

I have methods to (1) save the data to ionic storage, (2) Check if storage key exists, (3) Delete the storage key.我有方法(1)将数据保存到离子存储,(2)检查存储密钥是否存在,(3)删除存储密钥。
What I am trying to accomplish is hide the delete button if the storage key does not exist.如果存储密钥不存在,我想要完成的是隐藏删除按钮。 I have tried an *ngIf statement using the checkForKey() method but this just goes into an infinite loop.我已经使用 checkForKey() 方法尝试了一个 *ngIf 语句,但这只是进入了一个无限循环。 I have also tried ngShow, but this also has not worked for me.我也尝试过 ngShow,但这对我也不起作用。 Any help would be greatly appreciated.任何帮助将不胜感激。

ts ts

saveData(data) {
  this.storage.set( data.Name, JSON.stringify( data.Sections )).then(() => {
    this.listKeys();
  });
};

checkForKey(data) {
  this.storage.get(data).then(value => {
    if(value){
      console.log("true");
      return true
    }else{
      console.log("false");
      return false
    }
  }).catch(err=>{
    console.log(err)
  })
}

async deleteKeyValue(value: string) {
  const alert = await this.alertController.create({
    header: 'Confirm Deletion!',
    message: 'Delete this item <strong>?</strong>',
    buttons: [
      {
        text: 'Cancel',
        role: 'cancel',
        cssClass: 'secondary',
        handler: () => {
          console.log('Confirm Cancel: blah');
        }
      }, {
        text: 'DELETE',
        cssClass: 'delete',
        handler: () => {
          this.storage.remove(value).then(() => {
            this.listKeys();
          });
        }
      }

    ]
  });   
  await alert.present();  
}

You must never do this kind of method calls (ie checkForKey(data)) inside the Angular apps HTML template.你绝不能在 Angular 应用程序 HTML 模板中执行这种方法调用(即checkForKey(data)) Due to the change detection life cycle, it calls that method indefinitely and in the end, it directly leads to memory leaks and crashes the app.由于变更检测生命周期,它会无限期地调用该方法,最终直接导致内存泄漏并使应用程序崩溃。

You need to bring all the relevant data upfront here itemList.list and then use *ngIf like so:您需要将所有相关数据预先itemList.list ,然后像这样使用*ngIf

<div *ngIf="data.isTrue> .... </div>  

Note: data.isTrue - this is just a fiction property.注意: data.isTrue - 这只是一个虚构的属性。 Just changes it according to your app.只需根据您的应用程序更改它。

What I ended up doing was using *ngIf="i == data.task" in line thirteen.我最终做的是在第十三行使用 *ngIf="i == data.task" 。 So if the storage key does not exist所以如果存储密钥不存在

listKeys() {
  this.storage.keys().then((k) => {
    this.loop = k; 
  });
}

no div *ngFor="let i of loop" shows up.没有 div *ngFor="let i of loop" 出现。

<ion-card *ngFor="let data of itemList.list let i = index">
  <ion-card-header>
    <ion-card-title  class = "linkCenter">{{data.task}}</ion-card-title>
  </ion-card-header>

  <ion-card-content>
    <p>ID: <span style="color:#222428;" >{{data.formID}}</span></p>
    <p>Date: <span style="color:#222428;" >{{data.date}}</span></p>
    <p>Auditors: <span style="color:#222428;" ><b>{{data.auditors}}</b></span></p>
    <p>Inspection Type: <span style="color:#222428;" ><b>{{data.inspectionType}}</b></span></p>
    <div class = "linkCenter"> 
    <ion-button *ngIf= "data.downloadable == true" fill="outline" color="medium" (click)="saveData(data.link)" >Download</ion-button>
  </div>
    <div *ngFor="let i of loop">
      <div *ngIf="i == data.task">
        <div class = "linkButtons">
          <ion-button fill="outline" color="primary" (click)="openForm(data.Type, data.task)">&nbsp;&nbsp;Open&nbsp;&nbsp;</ion-button>
          <ion-button fill="outline" color="blackgreen" [disabled]="data.update == false" (click)="syncData()">Sync</ion-button>
          <ion-button fill="outline" color="danger" [disabled]="data.delete == false" (click)="deleteAudit( i )">Delete</ion-button>
        </div>
      </div>
    </div>
  </ion-card-content>
</ion-card> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM