mirror of
https://github.com/LucasVbr/todo-list.git
synced 2026-12-05 17:30:49 +00:00
Remake the todo-list with Angular
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import {Injectable} from "@angular/core";
|
||||
import {TodoItem} from "../models/todo-item.model";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class TodoItemService {
|
||||
todoItems: TodoItem[] = [];
|
||||
|
||||
get nextId(): number {
|
||||
return this.todoItems.length + 1
|
||||
}
|
||||
|
||||
getById(id: number): TodoItem {
|
||||
const todoItem = this.todoItems.find(item => item.id === id);
|
||||
if (!todoItem) throw new Error('Todo item not found!');
|
||||
return todoItem;
|
||||
}
|
||||
|
||||
newItem(text: string): void {
|
||||
const newTodoItem: TodoItem = new TodoItem(this.nextId, text);
|
||||
this.todoItems.push(newTodoItem);
|
||||
|
||||
this.saveItems()
|
||||
}
|
||||
|
||||
checkById(id: number, checked: boolean) {
|
||||
this.todoItems[id - 1].checked = checked;
|
||||
this.saveItems()
|
||||
}
|
||||
|
||||
loadItems() {
|
||||
const saveDataRaw: any = localStorage.getItem("todoList");
|
||||
|
||||
if (saveDataRaw as string) this.todoItems = JSON.parse(saveDataRaw);
|
||||
}
|
||||
|
||||
saveItems() {
|
||||
localStorage.setItem("todoList", JSON.stringify(this.todoItems))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user