Hello I am developing an Ionic app and I have an array that I want to push items on to it, but not lose the data when I change screens. Also, I do not want to use a database. Is there any other way? to add to an existing array and store that array locally?
你好,我正在开发一个离子应用程序,我有一个数组,我想把项目推到它上面,但当我改变屏幕时不会丢失数据。另外,我不想使用数据库。还有别的办法吗?要添加到现有数组并将该数组存储在本地?
$scope.tasksCollection = [
{ info: 'Go studying', measured: 'no', total: 1, done: 1, id: 1 },
{ info: 'Go to the beach', measured: 'no', total: 1, done: 1, id: 2},
{ info: 'Run', measured: 'yes', total: 30, done: 15, id: 3}
];
$scope.tasksCollection.push({
info: $scope.taskInfo,
measured: $scope.result,
total: total,
done: 0,
id: $scope.tasksCollection.length
})
The add function is working perfectly I just loose the data when changing states.
add函数工作得很好,我只是在改变状态时释放数据。
2 个解决方案
#1
2
If you want to keep data between controllers either use a service or local storage if you want to keep the data even when you quit the app.
如果你想在控制器之间保存数据,可以使用服务或本地存储,如果你想保存数据,即使你退出了应用程序。
Service example
Further angular documentation regarding services: https://docs.angularjs.org/guide/services
关于服务的进一步的角度文档:https://docs.angularjs.org/guide/services。
service.js:
service.js:
angular.module('yourmodule.services')
.service('Tasks', [function () {
var collection = {
tasks: []
};
return {
getTasks : function(){ return collection.tasks; }
}
}]
);
controller.js
controller.js
angular.module('yourmodule.controllers')
.controller('TaskCtrl', ['$scope', 'Tasks',
function($scope, Tasks){
$scope.Tasks = Tasks //Expose service to template
$scope.addTask = function(){
Tasks.getTasks().push({name : 'a new task'});
}
}]);
Local storage example
This is an excellent library which provides easy localstorage access for angularjs: https://github.com/grevory/angular-local-storage
这是一个优秀的库,为angularjs提供了方便的本地存储访问:https://github.com/grevory/angular-local存储
angular.module('yourmodule.controllers')
.controller('TaskCtrl', ['$scope', 'localStorageService',
function($scope, localStorageService){
$scope.collection = {
tasks : localStorageService.get('tasks') || [];
}
$scope.addTask = function(){
$scope.collection.tasks.push({name : 'a new task'});
localStorageService.set('tasks', $scope.collection.tasks);
}
}]);
#2
0
How about HTML5's locaStorage?
HTML5的locaStorage怎么样?
See Ionic formulas on Using Local Storage.
参见使用本地存储的离子公式。
#1
2
If you want to keep data between controllers either use a service or local storage if you want to keep the data even when you quit the app.
如果你想在控制器之间保存数据,可以使用服务或本地存储,如果你想保存数据,即使你退出了应用程序。
Service example
Further angular documentation regarding services: https://docs.angularjs.org/guide/services
关于服务的进一步的角度文档:https://docs.angularjs.org/guide/services。
service.js:
service.js:
angular.module('yourmodule.services')
.service('Tasks', [function () {
var collection = {
tasks: []
};
return {
getTasks : function(){ return collection.tasks; }
}
}]
);
controller.js
controller.js
angular.module('yourmodule.controllers')
.controller('TaskCtrl', ['$scope', 'Tasks',
function($scope, Tasks){
$scope.Tasks = Tasks //Expose service to template
$scope.addTask = function(){
Tasks.getTasks().push({name : 'a new task'});
}
}]);
Local storage example
This is an excellent library which provides easy localstorage access for angularjs: https://github.com/grevory/angular-local-storage
这是一个优秀的库,为angularjs提供了方便的本地存储访问:https://github.com/grevory/angular-local存储
angular.module('yourmodule.controllers')
.controller('TaskCtrl', ['$scope', 'localStorageService',
function($scope, localStorageService){
$scope.collection = {
tasks : localStorageService.get('tasks') || [];
}
$scope.addTask = function(){
$scope.collection.tasks.push({name : 'a new task'});
localStorageService.set('tasks', $scope.collection.tasks);
}
}]);
#2
0
How about HTML5's locaStorage?
HTML5的locaStorage怎么样?
See Ionic formulas on Using Local Storage.
参见使用本地存储的离子公式。