Skip to content

Instantly share code, notes, and snippets.

@Deiru2k
Created July 24, 2017 15:29
Show Gist options
  • Save Deiru2k/a5d820732c52d782b0fe2f3996653e8d to your computer and use it in GitHub Desktop.
Save Deiru2k/a5d820732c52d782b0fe2f3996653e8d to your computer and use it in GitHub Desktop.
<div>
<input [(ngModel)]="value" />
<button type="button" (click)="addValue(value)">Add</button>
</div>
import { Component, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'adder',
templateUrl: './template.html',
})
export class AdderComponent {
value: String = "";
@Output('onAdd') onAdd: EventEmitter<String> = new EventEmitter();
addValue = (): void => {
this.onAdd.emit(this.value);
this.value = "";
}
}
<div>
<adder (onAdd)="addItem($event)"></adder>
<ul>
<li *ngFor="let item of items">
{{item}}
</li>
<ul>
<div>
import { Component } from '@angular/core';
@Component({
selector: 'todo-list',
templateUrl: './TodoList.component.html',
styleUrls: ['./app.component.css']
})
export class TodoListComponent {
items: Array<String> = ['spam', 'eggs'];
addItem = (item: String): void => {
console.log(item);
this.items = [ ...this.items, item ];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment