Angular2 如何获取动态生成的 HTML 元素的引用 [英] Angular2 How to get reference of HTML elements generated dynamically

查看:27
本文介绍了Angular2 如何获取动态生成的 HTML 元素的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态生成了这些输入:

 

<!-- Material Design 输入--><md-input id="my-input-{{i}}"></md-input>

请注意 id=my-input-{{i}} 我想根据此动态 ID 获取对 DOM 元素的引用.此输入可以是 3、6 或更多输入,因此我需要动态访问 id 并保留它.

解决方案

唯一的回应是

let elem:Element = document.getElementById("myProgrammaticallyChosenIndex")

不需要其他棱角分明的奇怪仪式

在 angular 4+ 上测试

I have this inputs generated dynamically:

  <div *ngFor="let cell of column; let i = index;">
          <!-- Material design input-->
          <md-input id="my-input-{{i}}">
          </md-input>
  </div>

Please notice id=my-input-{{i}} I would like to get a reference to the DOM element based on this dynamic id. This input can be 3, 6 or more inputs so I need to access the id dynamically and get a hold to it.

解决方案

The only response is

let elem:Element = document.getElementById("myProgrammaticallyChosenIndex")

No other angular weird ceremony needed

Tested on angular 4+

这篇关于Angular2 如何获取动态生成的 HTML 元素的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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