Mohamed Abdelhady Mohamed Abdelhady - 4 years ago 113
AngularJS Question

ng storage of list at angular

i have a problem that i want to local storage a list and load it but
i found two problems that removing the item does not work well
and local storage is not working

.controller('tasksCtrl', [
'$scope',
'$stateParams',
'$localStorage',
function($scope, $stateParams) {
var dirlist = this;
var tasks = [
{
name: 'add task',
time: '12',
type: 'test'
}, {
name: 'do the home work',
time: '11',
type: 'study'
}
];
dirlist.list = tasks;
dirlist.toggle = false;
dirlist.addtask = function() {
dirlist.list.push({name: dirlist.name, time: dirlist.time, type: dirlist.type});
dirlist.name = '';
dirlist.time = '';
};
dirlist.remove = function(item) {
dirllist.splice(dirlist.list.indexOf(item), 1)
};

dirlist.saveData = function() {
$localStorage.list = tasks;
}
dirlist.loadData = function() {
dirlist.list = $localStorage.list;
}



}
])

Answer Source

local storage is not working

This is because you haven't passed the dependency in your controller. You are passing only scope and stateParams.

Here's how you can fix it,

.controller('tasksCtrl', [
  '$scope',
  '$stateParams',
  '$localStorage',
  function($scope, $stateParams, $localStorage) {

Now, $localStorage can be used as localStorage service.

Hope it helps.

Cheers coding!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download