Skip to content

Commit

Permalink
fixed formatting on init; fixed initial formatting without data in ng…
Browse files Browse the repository at this point in the history
… model; made validating the angular way
  • Loading branch information
mareczek committed Apr 20, 2015
1 parent 9c41abe commit 4184aaf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
27 changes: 14 additions & 13 deletions releases/international-phone-number.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(function() {
"use strict";
angular.module("internationalPhoneNumber", []).directive('internationalPhoneNumber', function() {
angular.module("internationalPhoneNumber", []).directive('internationalPhoneNumber', function($timeout) {
return {
restrict: 'A',
require: '^ngModel',
Expand Down Expand Up @@ -61,27 +61,28 @@
});
});
ctrl.$formatters.push(function(value) {
element.intlTelInput('setNumber', value);
return element.val;
if (!value) {
return value;
} else {
$timeout(function() {
return element.intlTelInput('setNumber', value);
}, 0);
return element.val();
}
});
ctrl.$parsers.push(function(value) {
if (!value) {
return value;
}
return value.replace(/[^\d]/g, '');
});
ctrl.$parsers.push(function(value) {
var validity;
if (value) {
validity = element.intlTelInput("isValidNumber");
ctrl.$setValidity('international-phone-number', validity);
ctrl.$setValidity('', validity);
ctrl.$validators.internationalPhoneNumber = function(value) {
if (!value) {
return value;
} else {
value = '';
delete ctrl.$error['international-phone-number'];
return element.intlTelInput("isValidNumber");
}
return value;
});
};
element.on('blur keyup change', function(event) {
return scope.$apply(read);
});
Expand Down
4 changes: 2 additions & 2 deletions releases/international-phone-number.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 16 additions & 14 deletions src/international-phone-number.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# https://github.com/mareczek/international-phone-number

"use strict"
angular.module("internationalPhoneNumber", []).directive 'internationalPhoneNumber', () ->
angular.module("internationalPhoneNumber", []).directive 'internationalPhoneNumber', ($timeout) ->

restrict: 'A'
require: '^ngModel'
Expand Down Expand Up @@ -50,37 +50,39 @@ angular.module("internationalPhoneNumber", []).directive 'internationalPhoneNumb
# Wait to see if other scope variables were set at the same time
scope.$$postDigest ->
options.defaultCountry = scope.defaultCountry

if newValue != null and newValue != undefined and newValue != ''
element.val newValue

element.intlTelInput(options)

unless attrs.skipUtilScriptDownload != undefined || options.utilsScript
element.intlTelInput('loadUtils', '/bower_components/intl-tel-input/lib/libphonenumber/build/utils.js')

watchOnce()

)


ctrl.$formatters.push (value) ->
element.intlTelInput 'setNumber', value
return element.val
if !value
return value
else
$timeout () ->
element.intlTelInput 'setNumber', value
, 0
return element.val()

ctrl.$parsers.push (value) ->
return value if !value
value.replace(/[^\d]/g, '')

ctrl.$parsers.push (value) ->
if value
validity = element.intlTelInput("isValidNumber")
ctrl.$setValidity 'international-phone-number', validity
ctrl.$setValidity '', validity
ctrl.$validators.internationalPhoneNumber = (value) ->
if !value
return value
else
value = ''
delete ctrl.$error['international-phone-number']
value
return element.intlTelInput("isValidNumber")


element.on 'blur keyup change', (event) ->
scope.$apply read
Expand Down

0 comments on commit 4184aaf

Please sign in to comment.