Skip to content

Instantly share code, notes, and snippets.

@jonathanwoahn
Last active June 4, 2019 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathanwoahn/84d15a9cab8fee43aa9fcafe5c9f496d to your computer and use it in GitHub Desktop.
Save jonathanwoahn/84d15a9cab8fee43aa9fcafe5c9f496d to your computer and use it in GitHub Desktop.
import { Component, ViewChild, ElementRef } from '@angular/core';
import { v4 as uuid } from 'uuid';
import { Todo } from './app.module';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
@ViewChild('todoInput') todoInput: ElementRef;
todos: Todo[] = [];
addTodo(event: KeyboardEvent): void {
if (event.keyCode !== 13) { return; }
const todo: Todo = {
id: uuid(),
text: this.todoInput.nativeElement.value,
};
this.todos.push(todo);
this.todoInput.nativeElement.value = '';
}
removeTodo(todo: Todo): void {
const index = this.todos.findIndex(t => t.id === todo.id);
this.todos.splice(index, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment