Skip to content

Instantly share code, notes, and snippets.

@GeekaholicLin
Created September 13, 2017 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeekaholicLin/9fc6bd747a477d84ffc4a340880f5039 to your computer and use it in GitHub Desktop.
Save GeekaholicLin/9fc6bd747a477d84ffc4a340880f5039 to your computer and use it in GitHub Desktop.
[angular-validation] Focus and scrollTo invalid field in Form,making non-input field under consideration
/* Edited by Geekaholic */
/* For scrollTo invalid form field*/
let timer = null;
$validationProvider.invalidCallback = function(element) {
element.closest('.form-group').addClass('has-error');
if(!timer){
timer = setTimeout(function () {
let isEditing = angular.element('input:focus')[0];//是否正在编辑,用户体验
if(!isEditing){
let offset = angular.element('.form-group.has-error').offset();
window.scrollTo(offset.left,offset.top);
element.closest('form').find('.form-group.has-error input')[0].focus();
}
clearTimeout(timer);
timer = null;
},300);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment