You can change this approach to solve this problem.
Possible Solution:
<label for="login-field-inputLogin" class="sr-only">Login</label> <input validate-onblur <--- directive, see below [(ngModel)]="login" id="login-field-inputLogin" class="form-control" placeholder="Login" ngControl="loginCtrl" #loginCtrl="ngModel" type="text" required /> <div [hidden]="loginCtrl.valid || loginCtrl.pristine" class="alert alert- danger">Login is required</div>
Directive Code:
@Directive({ selector: '[validate-onblur]', host: { '(focus)': 'onFocus($event)', '(blur)' : 'onBlur($event)' } }) export class ValidateOnBlurDirective { private hasFocus = false; constructor(public formControl: NgControl) { }
source share