Skip to content

Instantly share code, notes, and snippets.

@kevinmerckx
kevinmerckx / template.html
Created November 6, 2018 15:10
Tags usage as simple component
<app-tags [tags]="tags" (tagsChange)="tags = $event"></app-tags>
@kevinmerckx
kevinmerckx / template.html
Created November 6, 2018 15:04
Tags ideal usage
<app-tags [(ngModel)]="tags" [disabled]="…"></app-tags>
<app-tags [ngModel]="tags" (ngModelChange)="myCallback($event)"></app-tags>
<app-tags [formControl]="tagsControl"></app-tags>
<app-tags formControlName="tags"></app-tags>
@kevinmerckx
kevinmerckx / tags.component.ts
Created November 6, 2018 15:02
Tell Angular our component is a ControlValueAccessor
import { Component, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
@Component({
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => TagsComponent),
multi: true
}]
@kevinmerckx
kevinmerckx / tags.component.ts
Created November 6, 2018 15:00
Tags Component with Control Value Accessor
export class TagsComponent implements ControlValueAccessor {
onChange: (value: string[]) => void = () => {};
onTouched: Function = () => {};
isDisabled = false;
writeValue(value: string[]) {
this.tags = value || [];
}
@kevinmerckx
kevinmerckx / tags.component.html
Created November 6, 2018 14:13
Simple Tags Component
<ul>
<ng-template ngFor [ngForOf]="tags" let-tag let-index="index">
<li>
{{tag}}
<button type="button" (click)="remove(index)">x</button>
</li>
</ng-template>
</ul>
<input type="text"
@kevinmerckx
kevinmerckx / tags.component.ts
Last active November 6, 2018 14:12
Tags component which implements Control Value Accessor
import { Component, forwardRef, HostBinding } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
@Component({
selector: 'app-tags',
templateUrl: './tags.component.html',
styleUrls: ['./tags.component.sass'],
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => TagsComponent),