keypress event not working in ionic framework

2.2k views Asked by At

the truth is strange what happens. I'm trying to limit a textarea that takes a description. use the "keypress" event in the browser and everything worked well, but the application is not working. No receipt errors or anything like that, just not working.

var limite = 140;
$scope.resta = 0;
$('#counter').html("Quedan  "+limite+" caracteres");
  $("#text_view").keyup(function(e, $event){
      var box = $(this).val();
      var value = (box.length *100) / limite;
      $scope.resta = limite - box.length;
      if(box.length <= limite || $scope.resta > 0){
          $('#counter').html("Quedan  "+$scope.resta+" caracteres");
      }
      else{
          $('#counter').html("Quedan  "+$scope.resta+" caracteres");
          e.preventDefault();
          return;
      }               
  });

Also try keyup, this if it works, but in the textarea is still writing and not respesta the limit until the user presses a space.

1

There are 1 answers

1
Nabin Singh On BEST ANSWER

Its not considered good practice to mix Angularjs and Jquery code together. If you don't have any option then move jquery code to directive. Angularjs is really powerful so you can do rellay amazing stuffs without jquery help with less amount of code. You can achieve your requirement without even a single code of jquery as below.

HTML

<textarea ng-maxlength="{{limite}}" ng-change="trackChracterCount(box)" ng-model="box"></textarea>"Quedan  {{resta}} caracteres"

Javascript

$scope.limite = 140;
$scope.resta = $scope.limite;

var trackChracterCount = function(box){
    var limite = $scope.limite;
    var resta = $scope.resta;

    $scope.resta = limite - box.length;
}