角度设置组件的唯一参考编号 [英] angular setting unique reference #name to component

查看:47
本文介绍了角度设置组件的唯一参考编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的.这是重复的.

我对此要求完全相同:

如何设置唯一的模板引用变量* ngFor内?(角度)

但是,在上述问题中,确切的问题并未得到回答,但提供了替代方法.

However, in the issue above, the exact question was not answered but offered alternatives.

因此,我只要求这样做:甚至可以使用#"来指定唯一的名称(我找不到如何调用此方法,而是使用#来命名.有人知道吗,请吗?)

So, I ask only for this: Is it even possible to give unique names using "#" (I could not find how to call this method, giving name by #. Does someone know, please?)

我之所以这样问,是因为例如 mat-menu 与#names一起使用.

I ask this because, for example, mat-menu works with #names.

请在此处查看示例:

Angular Mat-Menu

如您所见,[matMenuTriggerFor]指的是#names.

As you see, [matMenuTriggerFor] refers to #names.

但是如何动态创建菜单呢?那我怎么用[matMenuTriggerFor]引用呢?

But what about creating the menus dynamically? Then how would I refer to it with [matMenuTriggerFor]?

推荐答案

Disclamer 我看不到问题的链接,答案是相同的,对不起

Disclamer I don't see the links of the question, it's the same answer, sorry

通常来说,在* ngFor傻瓜示例"中使用相同的模板引用是没有问题的

In general you has no problem indicate the same template reference in a *ngFor, "fool example"

<div *ngFor="let i of [1,2,3]">
   <input #input [value]="i"/>
   <button (click)="input.value=2*input.value">double</button>
</div>

对于输入"将其值加倍没有什么困惑,请参见堆栈闪电

There're no confused about what "input" double his value, see stackblitz

如果要在循环之外控制输入,则可以使用ViewChildren.在我们的.ts

If you want to control outside the loop the inputs, you can use ViewChildren. In our .ts

  @ViewChildren('input') inputs:QueryList<ElementRef>
  click()
  {
    this.inputs.forEach(x=>{
        x.nativeElement.value=2*x.nativeElement.value
    })
  }

还有一个按钮

<button (click)="click()">Double All</button>

注意:您可以在.html

NOTE: You can use directly "inputs" in the .html

<button (click)="this.inputs.first.nativeElement.value=
          2*this.inputs.first.nativeElement.value">
    Double first input
</button>

这篇关于角度设置组件的唯一参考编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆