简体   繁体   English

如何在不使用 Js id 选择器的情况下动态获取 Angular 中的 *ngFor 中的元素

[英]How to get an element in *ngFor in Angular dynamically without using Js id selector

I have a component that has a bootstrap dropdown, I want to focus on the current week that is set on the dropdown span我有一个带有 bootstrap 下拉菜单的组件,我想专注于下拉跨度上设置的当前周

I can do it with plain javascript by setting ids, and then using Jquery .focus() method to focus on it, But wanted to know if there is any angular 7/ 7+ way to do it using ViewChildren etc.我可以通过设置 ids 使用普通的 javascript 来完成它,然后使用 Jquery .focus() 方法来关注它,但是想知道是否有任何角度 7/ 7+ 的方法可以使用 ViewChildren 等来做到这一点。

<button class="btn dropdown-toggle"
        (click)="focusOnWeek(currentWeek)"
        type="button" data-toggle="dropdown">
  <span>Week {{currentWeek}}</span> // currentWeek = 5 need to focus on week 5 <a> tag on click of this span
</button>
<ul class="dropdown-menu">
  <li *ngFor="let week of weekList">//weekList = [1,2,3,4,5,6,7,8,9,10]>
    <a class="dropdown-item"
      Week {{week}} 
    </a>

  </li>
</ul>

On Click of Button the Currentweek is focused.单击按钮时,当前周是焦点。

You can use ViewChildren to find the anchor element that you want to focus.您可以使用ViewChildren来查找要聚焦的锚元素。 First, you set a template reference variable (eg #anchor ) on the anchor elements:首先,在锚元素上设置模板引用变量(例如#anchor ):

<ul class="dropdown-menu">
  <li *ngFor="let week of weekList">
    <a #anchor class="dropdown-item">Week {{week}}</a>
  </li>
</ul>

In the code, you get a reference to the anchor elements with ViewChildren :在代码中,您可以使用ViewChildren获得对锚元素的ViewChildren

@ViewChildren("anchor") anchorList: QueryList<ElementRef>;

and you set the focus on the anchor that corresponds to the specified week:并将焦点设置在与指定周对应的锚点上:

focusOnWeek(week) {
  const weekIndex = this.weekList.indexOf(week);
  const elementRef = this.anchorList.find((item, index) => index === weekIndex);
  elementRef.nativeElement.focus();
}

See this stackblitz for a demo.有关演示,请参阅此 stackblitz


If the menu is not immediately visible when the click is made, you can monitor the creation of the menu items with the QueryList.changes event.如果单击时菜单不是立即可见,您可以使用QueryList.changes事件监视菜单项的创建。 When you detect that the items are visible, you can then set the focus with currentWeek .当您检测到项目可见时,您可以使用currentWeek设置焦点。

ngAfterViewInit() {
  this.anchorList.changes.subscribe(() => {
    if (this.anchorList.length > 0) {
      const weekIndex = this.weekList.indexOf(this.currentWeek);
      const elementRef = this.anchorList.find((item, index) => index === weekIndex);
      elementRef.nativeElement.focus();
    }
  });
}

See this stackblitz for a demo.有关演示,请参阅此 stackblitz

Please add following code in html file请在html文件中添加以下代码

 <ul class="dropdown-menu">
      <li class="dropdown-item" [ngClass]="{'focus': currentWeek === week}" *ngFor="let week of weekList">
        <a>
          Week {{week}} 
        </a>

      </li>
    </ul>

add following class in css file在 css 文件中添加以下类

.focus{
   background-color: red;
}

Make sure you have change detection implemented in focusOnWeek() function.确保在 focusOnWeek() 函数中实现了更改检测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM