代码之家  ›  专栏  ›  技术社区  ›  Bernhard

在angular中通过模板级交互更新“autofocus”属性

  •  3
  • Bernhard  · 技术社区  · 7 年前

    对工作有问题吗 autofocus 属性与角度。在细节上我有一个 <form> <input type="text"> 在最上面,最初被一个条件聚焦。

    <input [attr.autofocus]="selection===-1"
           [(ngModel)]="myForm.firstInpElem" 
           name="firstInpElem" 
           placeholder="firstInpElem">
    

    然后窗体将在由 <input type="radio"> .

    一旦做出选择,就会显示相应的元素,然后应该得到 自动对焦 .

    但这并没有发生,我也不明白为什么会这样。

    准备 stackblitz with a working example 但主要是下面的标记将不会像预期的那样工作

    <h1>Forms example</h1>
    
    <form>
    
      <pre>Condition to focus "firstInpElem" is {{selection===1|json}}</pre>
    
      <p>This input element is autofocussed on page load</p>
    
      <p>
        <input [attr.autofocus]="selection===-1"
               [(ngModel)]="myForm.firstInpElem" 
               name="firstInpElem" 
               placeholder="firstInpElem">
      </p>
    
      <p>
        Provide one of both information:<br>
    
        <label>
          <input [(ngModel)]="selection" 
                 name="radioInpElem"
                 type="radio"
                 [value]="1"> 
                 Option 1
        </label>
        <br>
    
        <label>
          <input [(ngModel)]="selection" 
                 name="radioInpElem"
                 type="radio"
                 [value]="2">
                 Option 2
        </label>
    
      </p>
    
      <pre>Condition to focus "secondInpElem" is {{selection===1|json}}</pre>
      <pre>Condition to focus "thirdInpElem" is {{selection===2|json}}</pre>
      <p>
    
    
      <input *ngIf="selection===1"
            [attr.autofocus]="selection===1"
            [(ngModel)]="myForm.secondInpElem"
            name="secondInpElem"
            placeholder="secondInpElem">
    
    
    
      <input *ngIf="selection===2"
              [attr.autofocus]="selection===2"
              [(ngModel)]="myForm.thirdInpElem"
              name="thirdInpElem"
              placeholder="thirdInpElem">
      </p>
    </form>
    
    
    <pre>{{myForm|json}}</pre>
    
    2 回复  |  直到 7 年前
        1
  •  5
  •   ConnorsFan    7 年前

    如果您签入开发工具(F12工具),您将看到新的输入控件实际上获得了 autofocus 自动对焦 when the page loads . 在您的例子中,当新元素可见时,页面已经被加载。

    相反,您可以通过编程方式将焦点设置在新的输入元素上。为此,可以为具有 ngIf 条件:

    <input #inputElement *ngIf="selection === 1"
          [(ngModel)]="myForm.secondInpElem"
          name="secondInpElem"
          placeholder="secondInpElem">
    
    <input #inputElement *ngIf="selection === 2"
            [(ngModel)]="myForm.thirdInpElem"
            name="thirdInpElem"
            placeholder="thirdInpElem">
    

    并用计算机监控这些元素的存在 ViewChildren 以及 QueryList.changes 事件。每次一个输入元素变为可见时,您都会将焦点设置在该元素上:

    @ViewChildren("inputElement") inputElements: QueryList<ElementRef>;
    
    ngAfterViewInit() {
      this.inputElements.changes.subscribe(() => {
        this.inputElements.last.nativeElement.focus();
      });
    }
    

    看到了吗 this stackblitz

        2
  •  0
  •   Eliseo    7 年前

    另一个选项是隐藏输入(不使用*ngIf else)显示.样式)和参考变量 看见 https://stackblitz.com/edit/angular-sbc4pp?file=src%2Fapp%2Fapp.component.html

        <label>
          <input [ngModel]="selection" (change)="change(second,1)" 
                 name="radioInpElem"
                 type="radio"
                 [value]="1"> 
                 Option 1
        </label>
        <br>
        <label>
          <input [ngModel]="selection" (change)="change(third,2)"
                 name="radioInpElem"
                 type="radio"
                 [value]="2">
                 Option 2
        </label>
    
      <input #second [style.display]="selection===1?'inherit':'none'"
            [(ngModel)]="myForm.secondInpElem"
            name="secondInpElem"
            placeholder="secondInpElem">
    
      <input #third [style.display]="selection===2?'inherit':'none'"
              [(ngModel)]="myForm.thirdInpElem"
              name="thirdInpElem"
              placeholder="thirdInpElem">
    

      change(element:any,index:number)
      {
      this.selection=index;
      setTimeout(()=>{element.focus()},0);
      }