我一直在尝试使用AngStorage在我一直在使用的AngularJS应用中实现本地存储。调试我一直在寻找数据类型的问题和错误,即使一切似乎都已修复。

这是小问题:http://plnkr.co/edit/IpPDb4

// app.js

var app = angular.module('noteMate', ['ngRoute']);

app.controller('MainCtrl', function($scope, Notes) {

  $scope.notes = Notes.entries;
  $scope.tempNote = {};
  var index;

  $scope.save = function() {
    Notes.save($scope.tempNote);
    $scope.clear();
  };

  $scope.del = function(idx) {
    Notes.del(idx);
  };

  $scope.edit = function(idx) {
    $scope.tempNote = angular.copy($scope.notes[idx]);
    index = idx;
  };

  $scope.clear = function() {
    $scope.tempNote = {};
  };

  $scope.saveEdit = function() {
    $scope.notes[index] = $scope.tempNote;
  };

  $scope.mouse = false;

});

app.service('Notes', function(){
  this.saved = $localStorage.getItem('notes');
  this.entries = ($localStorage.getItem('notes')!==null) ? JSON.parse(this.saved) :
  [{title: "Hey", desc: "This is a sample note. Add your own note by clicking the button below. :)"}];

  $localStorage.setItem('notes',JSON.stringify(this.entries));

  this.save = function(entry) {
    this.entries.push(entry);
    $localStorage.setItem('notes',JSON.stringify(this.entries));
  };

  this.del = function(idx) {
    this.entries.splice(idx, 1);
    $localStorage.setItem('notes',JSON.stringify(this.entries));
  };

});


问题的任何解决方案和问题的解释?

最佳答案

1)把链接

  <script type="text/javascript" src="https://cdn.jsdelivr.net/ngstorage/0.3.6/ngStorage.min.js"></script>


2)需要ngStorage注入

  var app = angular.module('noteMate', ['ngRoute','ngStorage']);


检查示例Here

08-15 14:35