简体   繁体   English

如何为* ngFor中的项目创建不同的模板参考变量

[英]How to create different template reference variable for items in *ngFor angular

I have a list of button attributes and I want the buttons to have different template reference variables, but I'm rendering them using *ngFor. 我有一个按钮属性列表,我希望按钮具有不同的模板引用变量,但是我正在使用* ngFor渲染它们。 How can I set the reference variable to be #button1, #button2, etc. I need to set the template reference variable to be the ToolTipDirective for that button. 如何将参考变量设置为#button1,#button2等。我需要将模板参考变量设置为该按钮的ToolTipDirective。

Why I need to do this: The button's tooltip is not accessible in Firefox (the tooltip doesn't show when using TAB). 为什么需要执行此操作:在Firefox中无法访问按钮的工具提示(使用TAB时不显示工具提示)。 So, to make the tooltip show, I need the button to be: <button #button1=bs-tooltip (focus)="button1.toggle()" (blur)="button1.toggle()" [tooltip]=button.tooltip" >{{button.text}}</button> . Using the same template reference variable creates a problem where the tooltip for a button is shown when the focus is on another button (both tooltips are shown, but only 1 should show). 因此,要显示工具提示,我需要将按钮设置为: <button #button1=bs-tooltip (focus)="button1.toggle()" (blur)="button1.toggle()" [tooltip]=button.tooltip" >{{button.text}}</button> 。使用相同的模板引用变量会产生一个问题,即当焦点位于另一个按钮上时,将显示该按钮的工具提示(显示两个工具提示,但仅应显示1个)节目)。

Code: 码:

<li *ngFor="let button of buttons">
   <button #button1=bs-tooltip (focus)="onFocus(button1) [tooltip]=button.tooltip" >{{button.text}}</button> --> how can I set #button1?
</li>

you can bind id property and then use document to get the html element 您可以绑定id属性,然后使用document获取html元素

here is an example 这是一个例子

app.component.ts app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  onClick(foo) {
    console.log(foo)
  }

  getElementById(id) {
    return document.getElementById(id);
  }
}

app.component.html app.component.html

<button (click)="onClick(getElementById('foo'))">click </button>

<input [id]="'foo'">

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

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