未添加到离子存储中的项目

时间:2018-07-30 14:54:41

标签: angular ionic-framework ionic3 cordova-plugins

我正在尝试实现“收藏夹”功能,如果用户单击按钮,则会将一个项目添加到他的收藏夹中,我是说它们是离子存储的,但是没有得到保存。

这是我的服务,并且在此文件中。问题存在

favorites(item) {
    let items = [];

    this.storage.get("favorites").then((val) => {
      items = JSON.parse(val);
      if (items != null && items.length > 0) {
        if (items.indexOf(item) !== -1) {
          items.splice(item, 1);
        }
        else {
          items.push(item);
        }
      }
      else {
        let items = [];
        items.push(item);
      }
      this.storage.set('favorites', JSON.stringify(items));
    });
  }

然后在我的页面上

  likeChecker(item) { // check if the item is in favorites, if so, make the heat color read, by calling  like()
    let items = [];
    let counter = 0;
    this.storage.get("favorites").then((val) => {
      items = JSON.parse(val);
      if (items != null && items.length > 0) {
        if (items.indexOf(item) !== -1 && counter == 0) {
          console.log("item found");
          this.active = !this.active;
          counter++;
          console.log("item liked");
        }
      }
    });
  }
  // active like
  like(item) {
    this.active = !this.active;
    this.commonProvider.favorites(item);
  }

HTML

<button (click)="like(item)" ion-button icon-only>
    <ion-icon name="md-heart" [ngClass]="{'activeColor':active}"></ion-icon>
  </button>

因此,当您加载页面时,likeChecker会检查该项目是否已存储,如果是,则它将收藏夹按钮变为红色。那么每次您按like()时,它都会检查这些项目是否在收藏夹中,如果是,它将删除它,否则将其添加到数组中。

问题是,什么都没有添加到存储中。甚至console.log也不会被解雇。

0 个答案:

没有答案