I am pretty new to programming and I have a very frustrating problem. I really have spent hours to find an answer but I failed.
So basically I am working on a Master Detailed todo
list. My todos
are objects in an array. I am at the point where I should make the different views for the master-detail thing, but I can't separate the objects by ID.
Here is my AngularJS code:
myTodoList.controller("mainController", ['$scope', function($scope, $route, $routeParams, $location) {
$scope.todos = [{
'id': 0,
'title': '',
'done': false,
'priority': '',
'deadLine': '',
}];
$scope.$route = $route;
$scope.$location = $location;
$scope.$routeParams = $routeParams;
$scope.params = $routeParams;
$scope.todos = [];
$scope.addTodo = function(title, priority, deadLine, id) {
$scope.todos.push({
'id': $scope.todos.id,
'title': $scope.newTodo,
'done': false,
'priority': $scope.newPriority,
'deadLine': $scope.newDeadLine
});
$scope.newTodo = ''
$scope.newPriority = ''
$scope.newDeadLine = ''
$scope.todos.id++;
}
In this case when I try to create routes by :id it says that it is NAN (Not a Number I quess). But if I change the code to:
$scope.addTodo = function(title, priority, deadLine, id) {
$scope.todos.push({
'id': 0,
'title': $scope.newTodo,
'done': false,
'priority': $scope.newPriority,
'deadLine': $scope.newDeadLine
});
$scope.newTodo = ''
$scope.newPriority = ''
$scope.newDeadLine = ''
$scope.todos.id++;
}
This, so the 'id': 0
, then all todos get the number 0 as in ID. It's been 3 days since I can't make this thing right.
Not sure why you are using an array to hold the last id, but the problem is that you are not initializing the id;
var id;
id++; //NAN
Javascript is very lose as you can see, you can add properties to arrays and use operators on undefined variable without getting any error. In your case, $scope.todos.id = 0;
right after initializing the array should fix the problem.
But it would be better to hold the last id in another variable, and it doesn't need to be in the scope, just var lastId = 0;
and lastId++
is enough.