Skip to content

Instantly share code, notes, and snippets.

@andresmatasuarez
Last active August 29, 2015 14:15
Show Gist options
  • Save andresmatasuarez/8ae5ee691f78c4daad22 to your computer and use it in GitHub Desktop.
Save andresmatasuarez/8ae5ee691f78c4daad22 to your computer and use it in GitHub Desktop.
AngularJS | Directive | Limits an input length by trimming exceeding characters
'use strict'
app = angular.module 'app'
app.directive 'limitLength', ->
require : 'ngModel'
restrict : 'A'
link : ($scope, element, attrs, ngModel) ->
ngModel.$parsers.push (value) ->
if !_.isEmpty(value) and value.length > attrs.limitLength
value = ngModel.$modelValue
ngModel.$setViewValue value
ngModel.$render()
value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment