Created
March 27, 2012 12:23
-
-
Save petebacondarwin/2215441 to your computer and use it in GitHub Desktop.
AngularJS 1.0 - Masked Input Widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input ng-model="contact.phone" ui-mask='"(9999) 9999 9999"' placeholder="(01234)-5678-8901" required /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Widgets = angular.module('WidgetModule', []) | |
Widgets.directive 'uiMask', ()-> | |
require: 'ngModel' | |
scope: | |
uiMask: 'evaluate' | |
link: ($scope, el, attrs, controller)-> | |
$(el).mask($scope.uiMask) | |
# Add a parser that only allows the model value through if the mask is valid | |
controller.$parsers.push (value)-> | |
isValid = $(el).data('mask-isvalid') | |
controller.$setValidity('mask', isValid) | |
value = if isValid then el.mask() else null | |
# When the element blurs, update the viewvalue | |
$(el).bind 'blur', ()-> | |
$scope.$apply ()-> | |
controller.$setViewValue(el.mask()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment