代码之家  ›  专栏  ›  技术社区  ›  Pavan Jadda

cdkFocusInitial属性未聚焦DOM元素

  •  0
  • Pavan Jadda  · 技术社区  · 4 年前

    在Angular10项目中,我有一个表单,有两个输入,分别是用户名和密码。我正在使用 cdkFocusInitial https://stackblitz.com/edit/angular-4amzj4?file=src%2Fapp%2Ffocus-monitor-directives-example.html

    <div class="example-focus-monitor">
    
        <mat-form-field appearance="fill">
            <mat-label>Username</mat-label>
            <input cdkFocusInitial  aria-label="Username" class="username"
                 matInput
                placeholder="Enter Username" type="text">
      </mat-form-field>
    
            <br/>
            <mat-form-field appearance="fill">
        <mat-label>Password</mat-label>
        <input   aria-label=" Password" class="password"  matInput placeholder="Enter Password"
                type="text">
            </mat-form-field>
    </div>
    
    
      
    
    
      [1]: https://stackblitz.com/edit/angular-4amzj4?file=src%2Fapp%2Ffocus-monitor-directives-example.html
    
    0 回复  |  直到 4 年前
        1
  •  3
  •   PhilFlash    4 年前

    加载项第一个分区:

    cdkTrapFocus [cdkTrapFocusAutoCapture]="true" 
    

    https://stackblitz.com/edit/angular-4amzj4-nz2yh4?file=src%2Fapp%2Ffocus-monitor-directives-example.html

        2
  •  1
  •   Dharman chazomaticus    4 年前

    here .

    import { AfterContentInit, Directive, ElementRef, Input } from "@angular/core";
    
    @Directive({
      selector: "[autoFocus]"
    })
    export class AutofocusDirective implements AfterContentInit {
      @Input() public appAutoFocus: boolean;
    
      public constructor(private el: ElementRef) {}
    
      public ngAfterContentInit() {
        setTimeout(() => {
          this.el.nativeElement.focus();
        }, 100);
      }
    }
    

    <input autoFocus aria-label="Username" class="username"
                 matInput
                placeholder="Enter Username" type="text">
    

    如果你在一个页面上多次使用它,最后一次使用的那一个将会被聚焦。