, ! , maxLength , .
: https://jsfiddle.net/t1fhppc9/
ng-maxlength ($ scope.maxLength), angular ($ scope.message). (maxLength - message.length) . .
, ( user3214545). , , , .
:
<div ng-controller="myCtrl">
<textarea
ng-model="message"
ng-maxlength="{{ maxLength }}">
</textarea>
<div>
<span>Characters left: {{ remaining() }}</span>
</div>
If there are more than {{ maxLength }} characters, then a red border will be shown.
<div>
Message: {{ message }}
</div>
</div>
:
function myCtrl($scope) {
$scope.maxLength = 5;
$scope.remaining = function() {
return $scope.maxLength - angular.element(document).find('textarea')[0].value.length;
}
}
Here is the fiddle with function: https://jsfiddle.net/zz13p1tm/
Of course, in a more realistic scenario, this will be wrapped in a directive. Both the region and the element will be passed to the directive. Using the selection area, we can also control parameters such as the maximum length required, disabled, and whether other characters should be displayed in the template.
source
share