Javascript 在页面会话中存储喜欢的内容

Javascript 在页面会话中存储喜欢的内容,javascript,html,angularjs,Javascript,Html,Angularjs,我正在打印一个新闻条目列表,每个新闻条目都有“喜欢”和“不喜欢”按钮。是否有一种方法可以在页面会话中存储每个项目的喜欢/不喜欢?对此,我可以采取什么方法 JS: HTML: 新鲜事 {{n.author}}{{{formattate(n.publishedAt)}日期:'dd/MM/yyyy'} {{n.description | cut:true:100:'…'} //这是我喜欢和不喜欢的按钮 查看此服务以处理本地存储数据(它需要LoDash) (函数(){ "严格使用",; 角度。模

我正在打印一个新闻条目列表,每个新闻条目都有“喜欢”和“不喜欢”按钮。是否有一种方法可以在页面会话中存储每个项目的喜欢/不喜欢?对此,我可以采取什么方法

JS:

HTML:



新鲜事

{{n.author}}{{{formattate(n.publishedAt)}日期:'dd/MM/yyyy'}

{{n.description | cut:true:100:'…'}

//这是我喜欢和不喜欢的按钮
查看此
服务
以处理本地存储数据(它需要LoDash)

(函数(){
"严格使用",;
角度。模块('app')
.service('StorageSrv',['$rootScope',function($rootScope){
var self=这个,
前缀='应用程序名称',
ls=window.localStorage;
self.set=功能(键,val){
var obj=JSON.parse(ls.getItem(前缀))| |{};
_.设置(对象、键、值);
setItem(前缀,JSON.stringify(obj));
};
self.get=函数(键路径){
如果(!keyPath | |!|.size(keyPath))
返回JSON.parse(ls.getItem(前缀));
其他的
return u.get(JSON.parse(ls.getItem(前缀)),keyPath,null);
};
self.delete=函数(键路径){
var obj=JSON.parse(ls.getItem(前缀))| |{};
_.unset(对象,键路径);
setItem(前缀,JSON.stringify(obj));
};
}])

})();
你想过使用localStorage吗?@Jorgobregon不知道它是如何工作的tbh,我对Angular.var app=Angular.module('newsFeed',[])相当陌生这将重新创建模块并覆盖上一个模块?应该是var app=Angular.module('newsFeed'))当我使用上面的代码时,我得到一个错误
ReferenceError:StorageSrv未定义
?您必须加载StorageSrv代码块,然后将其注入控制器或服务(如
.controller('Newsfeed',function($scope,$http,StorageSrv){
),然后您就可以调用它了。
'use strict';

// Declare app level module which depends on views, and components
angular.module('newsFeed', [
    'ngRoute',
    'newsfeedapp.view1',
    'newsfeedapp.view2',
    'newsfeedapp.version',
    'newsFeed', ['angular.filter']

]).
config(['$locationProvider', '$routeProvider', function($locationProvider, $routeProvider) {
    $locationProvider.hashPrefix('!');
}]);

/* Newsfeed API Call */
var app = angular.module('newsFeed', [])
    .controller('Newsfeed', function($scope, $http) {
        $http.get('https://newsapi.org/v1/articles?source=the-next-web&sortBy=latest&apiKey=6ddf8d3cc8a54cc0abf89ad7d685da54').
        then(function(response) {
            $scope.news = response.data;
            console.log(response.data.articles);

        });
    });
<div class="container" ng-controller="Newsfeed">
  <br/>
  <form>
    <div class="col-md-6">
      <h2>Newsfeed</h2>
      <br/>
      <div class="form-group">
        <input type="text" class="form-control" ng-model="searchText" name="search-news" id="search-news" placeholder="Search for news">
      </div>
    </div>
  </form>


  <div>
    <div class="card" style="width: 20rem;" ng-repeat="n in news.articles | filter:searchText | filter:selectedAuthor">
      <img class="card-img-top img-responsive" src="{{n.urlToImage}}" alt="Card image cap">
      <div class="card-block">
        <h4 class="card-title"><a href="" ng-click="view($index)">{{n.title | cut:true:50:' ...'}}</a></h4>
        <p class="card-text"> {{n.author}} <small>on {{ formatDate(n.publishedAt) |  date:'dd/MM/yyyy'}}</small> </p>
        <p class="card-text"> {{n.description | cut:true:100:' ...'}}</p>

        // here are my like and dislike buttons
        <a class="btn btn-primary" href="#" role="button"><i class="fa fa-thumbs-up" aria-hidden="true"></i></a>
        <a class="btn btn-danger" href="#" role="button"><i class="fa fa-thumbs-down" aria-hidden="true"></i></a>
      </div>
    </div>
  </div>
</div>