我有一个带有模式的邮件字段:
pattern="^(([^<>()\[\]\.,;:\s@\']+(\.[^<>()\[\]\.,;:\s@\']+)*)|(\'.+\'))@(([^<>()[\]\.,;:\s@\']+\.)+[^<>()[\]\.,;:\s@\']{2,})$"
以及在同一字段中使用ngModel
的指令:
<input pattern="^(([^<>()\[\]\.,;:\s@\']+(\.[^<>()\[\]\.,;:\s@\']+)*)|(\'.+\'))@(([^<>()[\]\.,;:\s@\']+\.)+[^<>()[\]\.,;:\s@\']{2,})$"
dir= "ltr"
class="form-control sgn-rounded_textbox"
name="emailBox1"
type="email"
ng-model="vm.model.emails.emailField"
input-change = vm.mail>
inputChange指令:
(function () {
function inputChange($log, $timeout, $q, appCmdService) {
return {
restrict: 'A',
require: 'ngModel',
scope: {
inputChange: '='
},
link: function (scope, element, attrs, ngModel) {
var el = element[0];
if (checkForENSettings(scope)) {
if (ngModel) { // Don't do anything unless we have a model
ngModel.$parsers.push(function (value) {
if(value){
//some logic to change the value....
ngModel.$setViewValue(value);
// renders the input with the new viewValue
ngModel.$render();
//set cusor position
el.setSelectionRange(start, end);
//$log.log('ngModel.$parsers newVal', value);
return value;
}
});
ngModel.$formatters.push(function (value) {
ngModel.$setViewValue(value);
// renders the input with the new viewValue
ngModel.$render();
return value;
});
}
}
function checkForENSettings(scope){
if(scope.inputChange && scope.inputChange.lang === 'en'){
return true;
}
}
}
};
}
angular.module('common').directive('inputChange', inputChange);
})();
input-change
适用于没有pattern
的任何字段,但在合并它们时我会得到
Cannot assign to read only property 'message' of object '[object DOMException]'
由
提供Error: [$rootScope:inprog] $digest already in progress
有没有办法在ngModel
字段上使用pattern
和input
?
感谢。
答案 0 :(得分:1)
事实证明HTML5 input type="email"
存在问题,并且已知问题元素selectionStart
或selectionEnd
已知问题 -
HTML5 input email ans selection
删除type="email"
解决。
答案 1 :(得分:0)
你看过ng-pattern和ng-modal-options吗?
https://docs.angularjs.org/api/ng/directive/ngPattern https://docs.angularjs.org/api/ng/directive/ngModelOptions