Reputation: 1424
var app = angular.module('TodoApp', []);
app.controller('TodoCtrl', function($scope){
$scope.todos = ['test'];
$scope.done = function(todo){
var indexOf = $scope.todos.indexOf(todo);
if (indexOf !== -1) {
$scope.todos.splice(indexOf, 1);
}
};
$scope.add = function() {
$scope.todos.push($scope.newTodo);
$scope.newTodo= '';
};
});
I am new to angularjs and writing a simple todo list. I want the the data in $scope.todos to be dynamic so whenever a user moves to another page of my site and returns back he will get the same todos he has entered before. I am thinking of using PHP's session varible to store data into it and pass it to the angular app but I don't know how to do it or maybe this logic is not proper.I am new tho this so any new ideas to this would be awesome.
Upvotes: 1
Views: 4340
Reputation: 139
you can use cookie
or local storage
to store the user's todo list
cookie max size is small, but enough if you don't have too many todo
local storage max size is 5mb. you can try this angular lib https://github.com/grevory/angular-local-storage
make the todo list a global var by echo the json in your php file.
var g_todos = <?php echo json_encode($_SESSION['todo'] ?>;
in your controller
if(typeof g_todos === 'object' && g_todos.length){
$scope.todos = g_todos;
}else{
$scope.todos = [];
}
I think use cookie or localstorage is better, the second method is quite ugly....
Upvotes: 1