Angolare 1.3.19 comportamento modificato di ng-pattern
che interrompe la maschera.
Attualmente la direttiva ng-pattern convalida $viewValue
anziché $modelValue
- Reference in changelog.
Il team angolare ha fornito una direttiva personalizzata che ripristina il comportamento precedente. È buona soluzione per questo problema.
È necessario aggiungere l'attributo pattern-model
ai campi quando si utilizzano sia ui-mask
e ng-pattern
.
<input type="text"
class="form-control input-sm"
placeholder="hh:mm:ss"
name="hhmmss"
ng-model="data.hhmmss"
ng-pattern="/^([0-2]|0[0-9]|1[0-9]|2[0-3]):?[0-5][0-9]:?[0-5][0-9]$/"
ui-mask="99:99:99"
pattern-model
/>
Codice della direttiva (aggiungerlo al tuo codebase):
.directive('patternModel', function patternModelOverwriteDirective() {
return {
restrict: 'A',
require: '?ngModel',
priority: 1,
compile: function() {
var regexp, patternExp;
return {
pre: function(scope, elm, attr, ctrl) {
if (!ctrl) return;
attr.$observe('pattern', function(regex) {
/**
* The built-in directive will call our overwritten validator
* (see below). We just need to update the regex.
* The preLink fn guarantees our observer is called first.
*/
if (angular.isString(regex) && regex.length > 0) {
regex = new RegExp('^' + regex + '$');
}
if (regex && !regex.test) {
//The built-in validator will throw at this point
return;
}
regexp = regex || undefined;
});
},
post: function(scope, elm, attr, ctrl) {
if (!ctrl) return;
regexp, patternExp = attr.ngPattern || attr.pattern;
//The postLink fn guarantees we overwrite the built-in pattern validator
ctrl.$validators.pattern = function(value) {
return ctrl.$isEmpty(value) ||
angular.isUndefined(regexp) ||
regexp.test(value);
};
}
};
}
};
});
Problema in ui-maschera GitHub - Reference.
fonte
2016-11-18 21:26:02
Rimuovere il^dal modello ng e ha funzionato bene per me. – AndresL
La rimozione di^da ng-pattern non risolve il problema per me. – dps