I'm using ui-mask
from AngularJS-UI. I want to set the attribute dynamically depending on the HTML input type.
This is how you normally use this directive:
<input type="tel" class="form-control"
ng-model="Model.Homephone" placeholder="Home Phone"
ui-mask="(999) 999-9999" ui-mask-placeholder/>
It works fine like that. Now, I created a directive that just adds this attribute with it's value depending on the input type.
Custom Directive:
angular.module('App').directive("uiMaskType", ['$log', function ($log) {
return {
restrict: "A",
require: 'ngModel',
link: function (scope, element, attributes, ngModel) {
$log.log(element.attr('type'));
switch(element.attr('type')) {
case 'tel':
element.attr('ui-mask', '(999) 999-9999');
break;
case 'text':
element.attr('ui-mask', '999-99-9999');
break;
}
}
};
}]);
I use the directive inside the input element like this:
<input type="tel" class="form-control"
ng-model="Model.Homephone" placeholder="Home Phone"
ui-mask-type ui-mask-placeholder/>
When I run the page the result is this:
<input type="tel" class="form-control"
ng-model="Model.Homephone" placeholder="Home Phone"
ui-mask-type ui-mask-placeholder ui-mask="(999) 999-9999"/>
Even though the attribute is there, the masking is not working. I checked the AngularJS-UI's mask.js
file for the directive's priority and tried to set mine higher so that it adds the attribute before compiling the ui-mask
.
Anyone have an idea what could be the problem?