Created
July 26, 2015 17:48
-
-
Save akashrajkn/71e1a33a2a6355f7d83c to your computer and use it in GitHub Desktop.
angularjs directive to limit the length of message in a textbox
This file contains 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
/* directive to disable input if num(chars) crosses maxLength in text-area in pagePopUp */ | |
angular.module('notify.pageEOCApp').directive('textMaxlengthDirective', function() { | |
return { | |
require: 'ngModel', | |
link: function(scope, element, attrs, ngModelCtrl) { | |
var maxlength = Number(attrs.textMaxlength); | |
function fromUser(text) { | |
if(text.length > maxlength) { | |
var transformedInput = text.substring(0, maxlength); | |
ngModelCtrl.$setViewValue(transformedInput); | |
ngModelCtrl.$render(); | |
return transformedInput; | |
} | |
return text; | |
} | |
ngModelCtrl.$parsers.push(fromUser); | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in html , textarea style="resize: none;" rows='4' text-maxlength-directive="120" placeholder='Enter the message' ng-model="messageModel" class="form-control"></textarea