import * as _ from 'lodash'; export class ErrorScroll { constructor(private $anchorScroll: ng.IAnchorScrollService, private $location: ng.ILocationService) { } scrollToError(form: ng.IFormController) { const errorField = Object.keys(form).find(key => { const field = form[key]; // Check for both FE validations and BE service error validations return (_.has(field, '$invalid') && field.$invalid) || _.has(field, 'serviceError') }); if (errorField) { this.$location.hash(errorField); this.$anchorScroll(); } } }