Created
January 11, 2019 00:13
-
-
Save karkianish/aadae7433d3e60414a61c9440cd29f5d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { FormBuilder, FormGroup, FormArray } from '@angular/forms'; | |
@Component({ | |
selector: 'app-tag', | |
templateUrl: `<div> | |
<form [formGroup]='tagForm'> | |
<label for="tag-input">Category</label> | |
<input type="text" id="tagInput" formControlName='tagInput' (keyup)="onKeyUp($event)" > | |
<ul class="tag-list"> | |
<li *ngFor='let tag of tags' class="tag-item bg-info p-1 m-1 float-left text-white"> | |
{{tag}}<span class="remove-tag" (click)='onClick(tag)'> x</span> | |
</li> | |
</ul> | |
</form> | |
</div>`, | |
styleUrls: `span.remove-tag:hover { | |
background-color: red; | |
} | |
` | |
}) | |
export class TagComponent implements OnInit { | |
tagForm: FormGroup; | |
tags = ['test', 'jess']; | |
constructor(private fb: FormBuilder) { } | |
ngOnInit() { | |
this.tagForm = this.fb.group({ | |
tagInput: '', | |
}); | |
} | |
onKeyUp(event: KeyboardEvent) { | |
console.log(event); | |
const pressedEnterKey = event.keyCode === 13; | |
if (pressedEnterKey) { | |
const ctrl = this.tagForm.get('tagInput'); | |
this.tags.push(ctrl.value); | |
ctrl.reset(); | |
} | |
} | |
onClick(tagname: string): void { | |
console.log('yeha -', tagname); | |
this.tags = this.tags.filter(e => e !== tagname); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment