Angular 2 Focus on first invalid input after Click/Event

前端 未结 9 1709
不思量自难忘°
不思量自难忘° 2021-02-04 03:46

I have an odd requirement and was hoping for some help.

I need to focus on the first found invalid input of a form after clicking a button (not submit). The form is rath

9条回答
  •  迷失自我
    2021-02-04 04:50

    I've created an Angular directive to solve this problem. You can check it here ngx-scroll-to-first-invalid.

    Steps:

    1.Install the module:

    npm i @ismaestro/ngx-scroll-to-first-invalid --save
    

    2.Import the NgxScrollToFirstInvalidModule:

    import {BrowserModule} from '@angular/platform-browser';
    import {NgModule} from '@angular/core';
    import {NgxScrollToFirstInvalidModule} from '@ismaestro/ngx-scroll-to-first-invalid';
    
    @NgModule({
        imports: [
            BrowserModule,
            NgxScrollToFirstInvalidModule
        ],
        bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    3.Use the directive inside a form:

    Hope it helps! :)

提交回复
热议问题