Skip to content

Instantly share code, notes, and snippets.

@kamikaz1k
Last active July 23, 2016 15:51
Show Gist options
  • Save kamikaz1k/ef6890e689163f71f138561e39455996 to your computer and use it in GitHub Desktop.
Save kamikaz1k/ef6890e689163f71f138561e39455996 to your computer and use it in GitHub Desktop.
Phone Number component splitmodel function
link: function postLink (scope, element, attributes, ngModelCtrl) {
...
// Step 2.
// Set watcher on parentModel so that internal model is updated
scope.$watch("parentModel", function () {
splitModel();
});
// Function takes the parentModel value and splits it up
// to populate the internal data model
function splitModel () {
// If it is a US/CA country then update the dat
if (/US|CA/.test(scope.countryCode) && scope.parentModel) {
scope.phone.countryCode = scope.parentModel.substr(0,1);
scope.phone.areaCode = scope.parentModel.substr(1,3);
scope.phone.phoneOne = scope.parentModel.substr(4,3);
scope.phone.phoneTwo = scope.parentModel.substr(7,10);
}
// IF it is international, then update international model
else if (scope.parentModel) {
scope.phoneNumber.international = scope.parentModel;
}
// If parentModel has no value clear it
else {
scope.phone.countryCode = "1";
scope.phone.areaCode = "";
scope.phone.phoneOne = "";
scope.phone.phoneTwo = "";
scope.phone.international = "";
}
return;
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment