]>
Commit | Line | Data |
---|---|---|
92f5a8d4 TL |
1 | import { Directive, HostListener } from '@angular/core'; |
2 | import { NgControl } from '@angular/forms'; | |
3 | ||
f67539c2 | 4 | import _ from 'lodash'; |
92f5a8d4 TL |
5 | |
6 | @Directive({ | |
7 | selector: '[cdTrim]' | |
8 | }) | |
9 | export class TrimDirective { | |
10 | constructor(private ngControl: NgControl) {} | |
11 | ||
12 | @HostListener('input', ['$event.target.value']) | |
9f95a23c | 13 | onInput(value: string) { |
92f5a8d4 TL |
14 | this.setValue(value); |
15 | } | |
16 | ||
17 | setValue(value: string): void { | |
18 | value = _.isString(value) ? value.trim() : value; | |
19 | this.ngControl.control.setValue(value); | |
20 | } | |
21 | } |