Skip to content

Commit

Permalink
Merge pull request #4 from jonyw4/change-dynamically
Browse files Browse the repository at this point in the history
Tornando a mudança de mascara dinâmica possível
  • Loading branch information
wender committed Jan 21, 2016
2 parents 4b20002 + af50c05 commit 948553a
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/angular-simple-mask.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ angular.module('angularMask', [])
isModelValueEqualViewValues: '='
},
link: function ($scope, el, attrs, model) {
var format = attrs.angularMask,
arrFormat = format.split('|');

if (arrFormat.length > 1) {
arrFormat.sort(function (a, b) {
return a.length - b.length;
});
}
$scope.$watch(function(){return attrs.angularMask;}, function(value) {
if (model.$viewValue != null){
model.$viewValue = mask(String(model.$viewValue).replace(/\D/g, ''));
el.val(model.$viewValue);
}
});

model.$formatters.push(function (value) {
return value === null ? '' : mask(String(value).replace(/\D/g, ''));
Expand All @@ -30,7 +28,16 @@ angular.module('angularMask', [])
});

function mask(val) {
if (val === null) {
var format = attrs.angularMask,
arrFormat = format.split('|');

if (arrFormat.length > 1) {
arrFormat.sort(function (a, b) {
return a.length - b.length;
});
}

if (val === null || val == '') {
return '';
}
var value = String(val).replace(/\D/g, '');
Expand Down

0 comments on commit 948553a

Please sign in to comment.