Mascara AngularJS

0

I am using AngularJS and in my form I have some fields that need masks, until then I have already done it, I used ui.mask .

var app = angular.module("empresaApp",['angularUtils.directives.dirPagination','ui.mask']);

My input looks like this:

<input type="text" ng-model="empresa.nr_CNPJ" ui-mask="99.999.999/9999-99"  />

But when I enter the data, in this field for example it is 22222222222222 and does not apply the mask, someone has had a problem with it or knows how to solve it. Do I need to save in the database already with the mask?

    
asked by anonymous 26.09.2017 / 21:01

1 answer

0

In ui-mask the data format is only in the element that in this case is a <input> in the model is the raw data, but there is a way to retrieve the formatted value:

angular.element('#id_do_elemento').val()

With angular.element comes the value with the mascara, look at a minimum example :

var app = angular.module('app', ['ui.mask']);

app.controller('ctrl', function($scope) {
  $scope.text = "11111111111111";
  $scope.read = function() 
  {    
    console.log(angular.element('#text').val());    
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-mask/1.8.7/mask.min.js"></script><divng-app="app" ng-controller="ctrl">
  <input id="text" type="text" ng-model="text" ui-mask="99.999.999/9999-99" ui-mask-placeholder-char="_" />
  <button ng-click="read()">Log</button>
</div>

26.09.2017 / 21:50