Skip to content

Instantly share code, notes, and snippets.

@victormejia
Last active January 4, 2016 20:29
Show Gist options
  • Save victormejia/8674005 to your computer and use it in GitHub Desktop.
Save victormejia/8674005 to your computer and use it in GitHub Desktop.
phone input directive for AngularJS
<div class="phoneFields">
<input type="text" id="phone" ng-model="area" maxlength="3"> -
<input type="text" ng-model="prefix" maxlength="3"> -
<input type="text" ng-model="suffix" maxlength="4">
</div>
angular.module('app')
.directive('phoneInput', ['$log',
function ($log) {
return {
restrict: 'EA',
templateUrl: 'partials/directives/phoneInput.html',
replace: true,
scope: {
area: '=',
prefix: '=',
suffix: '='
},
link: function (scope, element, attrs, controller) {
var elems = element.find('input');
var prefixElem = angular.element(elems[1]),
suffixElem = angular.element(elems[2]);
scope.$watch('area', function (val) {
if (val && val.length === 3) {
prefixElem.focus();
}
});
scope.$watch('prefix', function (val) {
if (val && val.length === 3) {
suffixElem.focus();
}
});
}
}
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment