角度 - 本地存储无法正常工作

时间:2016-02-26 18:27:15

标签: javascript angularjs ionic-framework

我正在尝试使用AngularJS ngStorage模块加载应用时将数据存储在本地存储中。

我有以下代码:

app.js:

angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.services', 'app.directives', 'ngCordova', 'ngStorage'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }

    $localStorage.message = "Hello World";

  });
})

然后使用我的控制器,我想做以下事情。

controller.js:

$scope.data = $localStorage.message;

最后输出保存的信息。

message.html

<div><p> Saved Message:{{ data}} </p></div>

当我使用Chrome Inspect工具检查本地存储时,我看不到存储的消息。知道如何在启动时从app.js保存数据吗?

6 个答案:

答案 0 :(得分:1)

根据文件: -

 var app = angular.module('app', ['ngStorage'])
    .config(['$localStorageProvider',
        function ($localStorageProvider) {
            $localStorageProvider.get('MyKey');//gets

            $localStorageProvider.set('MyKey', { k: 'value' });/sets
        }]);

 localStorage.setItem('message', 'helloworld');//saves in developer tools
 $scope.getItem=localStorage.getIem('message');

答案 1 :(得分:1)

您需要在run方法和控制器中注入$ localStorage服务。像这样:

angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.services', 'app.directives', 'ngCordova', 'ngStorage'])

.run(function($ionicPlatform, $localStorage) {
  $ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
  // org.apache.cordova.statusbar required
  StatusBar.styleDefault();
}

$localStorage.message = "Hello World";

  });
})

见第三行。运行函数参数。

答案 2 :(得分:1)

只需使用localStorage而非$localStorage因为$localStorage必须注明,而localStorage默认来自angularjs而且无需注入任何内容。我已经研究过了。

$ionicPlatform.ready(function() {
    localStorage.message = "whatever";
});

在控制器中:

$scope.message = localStorage.message;

在视图中

<div>{{message}}</div>

答案 3 :(得分:1)

您忘了声明$ localStorage变量,如下所示:

        Document = new PdfDocument(pageWidth, pageHieght, unitOfMeasure, FileName);
        PdfFont ArialNormal = new PdfFont(Document, "Arial", FontStyle.Regular, false);
        Page = new PdfPage(Document, pageWidth, pageHieght);

        Contents = new PdfContents(Page);
        Contents.SetColorNonStroking(Color.Black);
        BarcodeEAN13 Barcode1 = new BarcodeEAN13(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3 });
        Contents.DrawBarcode(PosX, PosY, TextJustify.Center, 40, 12, Barcode1, ArialNormal, 12);

答案 4 :(得分:0)

我正在研究离子和使用localstorage(简单的html本地存储功能) 在localStroage中设置值

window.localStorage['Invite-Code'] = $scope.passCode;

上面的代码设置了本地存储中的值。

 var val = window.localStorage['Invite-Code'] || false;

上面的代码显示了localstorage值的值,如果存在与否则则其返回值为true或false。这里'邀请代码'是关键

答案 5 :(得分:0)

<强> app.js

var app = angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.services', 'app.directives', 'ngCordova', 'ngStorage'])

然后在你的 controller.js 中你需要注入$localStorage

app.controller('yourController', ['$scope', '$localStorage', function ($scope, $localStorage) {
    $scope.data = $localStorage.message;
}]);