LocalStorage在Android上无法正常运行,可在浏览器中使用

时间:2016-05-09 02:50:49

标签: javascript android angularjs ionic-framework local-storage

我目前正在尝试使用 Ionic AngularJS 创建一个Checklist(待办事项)应用,以便在Android设备上运行。

当我通过ionic serve在浏览器中测试我的应用时,数据存储正常并且恢复正常。但是,在我的三星Galaxy Note 5上运行时,该应用程序在尝试存储数据时似乎无法运行。

这是我的app.js,非常感谢任何帮助,我存储数据和恢复数据的部分是var storage的部分。

// Ionic Checklist App
angular.module('checklist', ['ionic', 'ngCordova'])


.run(function ($ionicPlatform, TaskService) {
    $ionicPlatform.ready(function () {
        if (cordova.platformId === 'ios' && window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            cordova.plugins.Keyboard.disableScroll(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }

    });
    var storage = window.localStorage;
    var tasks = JSON.parse(storage.getItem('tasks'));
    console.log(tasks)
    TaskService.setTasks(tasks);
    console.log("loaded tasks");
})

.controller('TasksCtrl', function ($scope, $ionicModal, $cordovaFile, TaskService) {
    $scope.tasks = TaskService.getTasks();
    var storage = window.localStorage;

    $ionicModal.fromTemplateUrl('newTaskModal',
        {
            scope: $scope,
            animation: 'slide-in-up'
        }).then(function (modal) {
            $scope.modal = modal;
        });

    $scope.openNewTask = function () {
        $scope.modal.show();

    }

    $scope.closeNewTask = function () {
        $scope.modal.hide();
    }

    $scope.newTaskInput = {
        taskName: '',
        taskComments: ''
    }

    $scope.newTask = function () {
        console.log($scope.newTaskInput.taskName);
        TaskService.makeTask($scope.newTaskInput.taskName, $scope.newTaskInput.taskComments);
        storage.setItem('tasks', JSON.stringify($scope.tasks));
        $scope.closeNewTask();
    }

    $scope.deleteTask = function (task) {
        console.log(TaskService.getTasks());
        TaskService.deleteTask(task);
        storage.setItem('tasks', JSON.stringify($scope.tasks));
    }

})

.controller('TaskDetailCtrl', function ($scope, $stateParams, TaskService) {
    $scope.taskId = $stateParams.taskId;
    $scope.task = TaskService.getTask($scope.taskId);
})

.config(function ($stateProvider, $urlRouterProvider, $ionicConfigProvider) {

    $ionicConfigProvider.navBar.alignTitle('center');

    $stateProvider
        .state('tasks', {
            url: '/',
            templateUrl: 'tasks',
            controller: 'TasksCtrl'
        })
        .state('taskDetail', {
            url: '/:taskId',
            templateUrl: 'taskDetail',
            controller: 'TaskDetailCtrl'
        });

    $urlRouterProvider.otherwise("/");

})

.service('TaskService', function () {
    return {
        //Sample Tasks
        /*tasks: [
        {
            id: '1',
            name: 'Milk',
            comment: 'get it from the Frick Park Market'
        }, 
        {
            id: '2',
            name: 'Eggs',
            comment: 'get from chicken'
        }
        ],*/
        tasks: [],
        setTasks: function(restore) {
            this.tasks = restore;
        },
        makeTask: function(name, comment) {
            var newId = this.tasks.length + 1;
            this.tasks.unshift({
                id: newId,
                name: name,
                comment: comment
            });
        },
        deleteTask: function (task) {
            console.log(task);
            console.log(this.tasks);
            console.log(this.tasks.indexOf(task));
            this.tasks.splice(this.tasks.indexOf(task), 1);
            for (i = 0; i < this.tasks.length; i++) {
                this.tasks[i].id = i+1;
            };
        },
        getTasks: function() {
            return this.tasks;
        },
        getTask: function (taskId) {

            for (i = 0; i < this.tasks.length; i++) {

                if (this.tasks[i].id == taskId) {
                    return this.tasks[i];
                }

            }

        }
    }
})

0 个答案:

没有答案