Last active
March 22, 2018 17:44
-
-
Save tperrelli/153bb85387425bb568007cd99a8ad85d to your computer and use it in GitHub Desktop.
Keypress input limit
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 { 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