I' working on angular directive, to show error msg on exceeding character count on textarea.
with following code, I can show error(div) MSG by appending on element, but I cannot hide it after showing it for first time, what I need to do in else block to remove the "elCharLimitError".
angular.module('app', []).directive('wordCount', ['$compile', function($compile) {
function wordCountLinkFunc(scope, ele, attr, ngModel) {
var charLimit = attr.wordCount;
console.log(charLimit);
var elCharLimitError = angular.element('<div class="alert alert-danger">Cannot exceed Character limit<b>' + charLimit + '</b></div>');
ele.bind('keyup', function(event) {
if (ngModel.$viewValue && ngModel.$viewValue.length >= charLimit) {
$compile(elCharLimitError)(scope);
ele.append(elCharLimitError);
} else {
//TODO: need help to hide the error element "elCharLimitError"
}
});
ele.bind('keypress', function(event) {
if (ngModel.$viewValue && ngModel.$viewValue.length >= charLimit) {
if (event.keyCode !== 8) {
event.preventDefault();
}
} else {
//TODO: need help to hide the error msg "elCharLimitError"
}
});
}
return {
restrict: 'A',
require: 'ngModel',
link: wordCountLinkFunc
};
}]);
plunker Link : http://plnkr.co/edit/OjfsfFwkeiJKlnHZ9TOE