以下代码不起作用..
<input type="text"
class="form-control input-sm"
placeholder="hh:mm:ss"
name="hhmmss"
ng-model="data.hhmmss"
ui-mask="99:99:99"
ng-pattern="/^([0-2]|0[0-9]|1[0-9]|2[0-3]):?[0-5][0-9]:?[0-5][0-9]$/"
/>当输入值为20:00:00时,formName.hhmmss.$error.pattern为true。
如果删除ui-mask
<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]$/"
/>当输入值为20:00:00时,formName.hhmmss.$error.pattern为false。
如何在ng-pattern中使用正则表达式
发布于 2015-02-17 05:15:18
我也遇到了同样的问题,并修改了mask.js文件以更新keypress的作用域值。有一行代码可以做到这一点,但并不是一直都在运行。
controller.$setViewValue(valUnmasked);将if语句更新为以下内容:
if (valAltered || iAttrs.ngPattern) {这将在按键时运行"scope.apply“并更新模型。
发布于 2016-11-19 05:26:03
Angular 1.3.19改变了ng-pattern的行为,破坏了用户界面掩码。
目前,ng-pattern指令验证$viewValue而不是$modelValue - Reference in changelog。
Angular团队提供了自定义指令来恢复之前的行为。这是解决此问题的好方法。
当同时使用ui-mask和ng-pattern时,必须将pattern-model属性添加到字段中。
<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
/>指令代码(将其添加到代码库):
.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);
};
}
};
}
};
});ui-mask GitHub - Reference中的问题。
https://stackoverflow.com/questions/28207033
复制相似问题