Skip to content

Instantly share code, notes, and snippets.

@tperrelli
Last active March 22, 2018 17:44
Show Gist options
  • Save tperrelli/153bb85387425bb568007cd99a8ad85d to your computer and use it in GitHub Desktop.
Save tperrelli/153bb85387425bb568007cd99a8ad85d to your computer and use it in GitHub Desktop.
Keypress input limit
import { Directive, Input } from '@angular/core';
@Directive({
selector: '[limit]',
host: {
'(keypress)': '_onKeypress($event)',
}
})
export class LimitDirective {
@Input('limit') limit;
_onKeypress(e) {
const limit = +this.limit;
console.log('limit:', limit);
console.log('input:', e.target.value.length);
if (e.target.value.length === limit) e.preventDefault();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment