javascript - AngularJS Directive how to show/hide custom(appended) element on a condition -


i' working on angular directive, show error msg on exceeding character count on textarea.

with following code, can show error(div) msg appending on element, cannot hide after showing first time, need in else block remove "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 hide error element "elcharlimiterror"             }         });         ele.bind('keypress', function(event) {              if (ngmodel.$viewvalue && ngmodel.$viewvalue.length >= charlimit) {                 if (event.keycode !== 8) {                     event.preventdefault();                 }             } else {                 //todo: need hide error msg "elcharlimiterror"             }         });     }      return {         restrict: 'a',         require: 'ngmodel',         link: wordcountlinkfunc     }; }]); 

plunker link : http://plnkr.co/edit/ojfsffwkeijklnhz9toe

first of all, don't want append new content in textarea: it's not supposed have html content in , can't display it. instead consider inserting error message right after element.

for show/hide functionality extract 2 separate functions , use them inside event handler.

directive this:

angular.module('app', []).directive('wordcount', ['$compile', function($compile) {       function wordcountlinkfunc(scope, ele, attr, ngmodel) {          var charlimit = attr.wordcount;         var elcharlimiterror = angular.element('<div class="alert alert-danger">cannot exceed character limit<b>' + charlimit + '</b></div>');          function showerror() {             if (!elcharlimiterror.hasclass('ng-scope')) {                 $compile(elcharlimiterror)(scope);                 ele.after(elcharlimiterror);             }             elcharlimiterror.show();         }          function hideerror() {             elcharlimiterror.hide();         }          ele.bind('keyup', function(event) {             if (ngmodel.$viewvalue && ngmodel.$viewvalue.length >= charlimit) {                 showerror();             } else {                 hideerror();             }         });          ele.bind('keypress', function(event) {              if (ngmodel.$viewvalue && ngmodel.$viewvalue.length >= charlimit) {                 if (event.keycode !== 8) {                     event.preventdefault();                 }             } else {                 hideerror();             }         });     }      return {         restrict: 'a',         require: 'ngmodel',         link: wordcountlinkfunc     }; }]); 

demo: http://plnkr.co/edit/dkrwtblj8ikoskodwrby?p=preview


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -