【问题标题】:Save id to local storage with button angularjs使用按钮 angularjs 将 id 保存到本地存储
【发布时间】:2016-03-31 02:11:37
【问题描述】:

我在我的项目中使用了最喜欢的按钮。这是demo

script.js

angular.module("MyApp",[]);

service.js

angular
.module("MyApp")
.service("FavoriteService",[function(){


//Favorites functions
this.isLocalStorageEnable = function() {

    if(typeof (Storage) !== "undefined"){
        return true;
    }
    else{
        return false;
    }
};

this.isFavorite = function(scope){
    var fav = localStorage.getItem(scope.id);
    return fav === "1";
};


this.setFav = function(scope){
    localStorage.setItem(scope.id,"1");

};


this.deleteFav = function(scope){
    localStorage.removeItem(scope.id);

};


 }]);

directive.js

angular.module("MyApp").directive("albumDirective",    ["FavoriteService", function(FavoriteService) {

return {
restrict: "AE",
templateUrl: "AlbumDirective.html",
replace: true,
scope: {
    id: "=albumId"
},
  link: function(scope)
     {
      scope.isLocalStorageEnable = FavoriteService.isLocalStorageEnable;
      scope.isFavorite = FavoriteService.isFavorite(scope);

    scope.markAs = function(type) {
      switch(type) {
        case true :
          FavoriteService.setFav(scope);
          break;
        case false : 
          FavoriteService.deleteFav(scope);
          break;

      }
      scope.isFavorite = type;
    }

}

  };

  }]);

AlbumDirective.html

        <button ng-click="markAs(!isFavorite)" ><i class="fa" ng-class="{'fa-heart' : isFavorite , 'fa-heart-o' : !isFavorite }"></i></button>

Itempage.html

<album-directive album-id="1"></album-directive>

当我在 ItemPage.html 中单击第 1 项的心形按钮时,该按钮变为活动状态,当我再次导航到第 2 项的 ItemPage.html 时,该按钮已处于活动状态。在每个项目页面中,我都有 {{object.itemid }} 。我想将 itemid 添加到本地存储中,因此该按钮仅在为每个项目按下按钮时才处于活动状态。我从未使用过本地存储。有什么建议吗?在此先感谢。

【问题讨论】:

  • item2 的 item id 是什么?您的示例似乎工作正常,只要每个项目具有不同的 id 就不会有任何问题
  • 当我在 itempage 并查看项目 1 并单击按钮将 item1 设置为收藏时,项目 1 的 itemid=1,item1 的 itemid=2 是有效的。但是,当我返回到 itempage.html 以查看 item2 时,该按钮已经处于活动状态。所以所有项目只有一种状态(活动或非活动)。我需要为每个项目一个状态。

标签: angularjs local-storage


【解决方案1】:

安东尼,

Angular 服务是单例的,因此在应用程序的生命周期中只被实例化一次。这意味着它们非常适合跨控制器存储和共享数据。

本地存储

The localStorage object 存储没有过期日期的数据。关闭浏览器不会删除数据,次日、次周或次年可用。

因此,行为。打开多个网页将访问同一个本地存储,它本身就是一个单例。

不同之处在于,使用 Angular,每个打开的页面都有自己的应用程序实例。因此,每个网页都会有自己的服务实例。

解决方案

Here is the plunker

守则

angular.module("MyApp").service("FavoriteService",[function(){


  this.fav = 0;

  //Favorites functions
  this.isLocalStorageEnable = function() {

      if(typeof (Storage) !== "undefined"){
          return true;
      }
      else{
          return false;
      }
  };

  this.isFavorite = function(scope){
      return this.fav === 1;
  };

  this.setFav = function(){
      this.fav = 1;

  };

  this.resetFav = function(){
      this.fav = 0;

  };

}]);

【讨论】:

  • 感谢您的回答。此解决方案无法解决问题。我已经遇到了同样的问题(按钮在所有页面中都处于活动状态),现在按钮处于活动状态,再次单击时保持活动状态。
  • 我在 Chrome 中使用 plunker,它就像一个魅力。你在用这个插件吗:plnkr.co/edit/MdtaYFTIKdiFz4csTF5O?p=preview
  • 是的。当我单击按钮时,按钮变为活动状态,然后对所有项目都处于活动状态
  • 我很抱歉。该按钮一旦设置就处于活动状态,因为如果您有多个项目使用相同的服务,它们将使用相同的变量值。您可能希望每个项目都有一个服务变量。在这种情况下,因为可能有这么多的项目,那么数组就可以工作,或者某种列表
猜你喜欢
  • 2021-12-25
  • 2015-03-22
  • 1970-01-01
  • 2018-11-05
  • 2016-04-29
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多