A good way to do this is to use the directive. Here's how to do it:
View:
<div ng-app="foo" ng-controller="fooController"> <textarea text-length-handler="doThing()" text-length="6" ng-model="text"> </textarea> </div>
JS:
angular.module('foo', []) .directive('textLength', function(){ return { restrict: 'A', require: 'ngModel', scope: { textLengthHandler: '&' }, link: function ($scope, $element, $attrs, ctrl) { var limit = parseInt($attrs.textLength); var handler = function(){ if (ctrl.$modelValue.length >= limit) { $scope.textLengthHandler() } }; $element.on('keypress', handler);
Spell here:
http://jsfiddle.net/dtq0mz8m/
bioball
source share