简体   繁体   English

要在DOM中查找元素而不使用在angular 2+中使用* ngFor生成的document.getElementById()

[英]To find the element in DOM without using document.getElementById() generated using *ngFor in angular 2+

app.component.html: app.component.html:

<div *ngFor = "let ivalue of arraytoDispaly;let i = index;">
      <input type="text" id={{i}} [value]="ivalue.inputValue" />
</div>
<button (click)='GetElementValue()'>GetElementValue</button>

app.component.ts: app.component.ts:

arraytoDispaly = [{'inputValue':'abc'},
                  {'inputValue':'def'},
                  {'inputValue':'ghi'}];
GetElementValue(){
 var t = (<HTMLInputElement>document.getElementById('1')).value;
 console.log(t);
}

Whenever I am clicking on 'GetElementValue' button, I am getting the value of input field with id of '1' as 'def'. 每当我单击“ GetElementValue”按钮时,我都会获得ID为“ 1”的输入字段的值作为“ def”。 Need to know is there any other way to access element generated by *ngFor in angular. 需要知道还有其他方法可以访问* ngFor生成的angular元素。 Thanks 谢谢

Tag your inputs with a template variable: 使用模板变量标记您的输入:

<div *ngFor = "let ivalue of arraytoDispaly;let i = index;">
      <input #inputs type="text" id={{i}} [value]="ivalue.inputValue" />
</div>
<button (click)='GetElementValue()'>GetElementValue</button>

Then you can use @ViewChildren and query for the ElementRef using the QueryList API: 然后,您可以使用@ViewChildren并使用QueryList API查询QueryList

@ViewChildren('inputs', 
    { read: ElementRef }) inputs: QueryList<ElementRef>;

The possible values for read are: ElementRef , ViewContainerRef or name of the Component/Directive itself (useful if you have more than one directive on an element). 可能的read值是: ElementRefViewContainerRef或Component / Directive本身的名称(如果在一个元素上有多个指令,则很有用)。

Initialize the elements if you need to by implementing AfterViewInit : 如果需要,可以通过实现AfterViewInit来初始化元素:

ngAfterViewInit(): void {
   // so something with inputs
}

You also have access to inputs via a click handler: 您还可以通过点击处理程序访问inputs

GetElementValue(){
   var t = (<HTMLInputElement> this.inputs.toArray()[1].nativeElement).value;
   console.log(t);
}

Or more succinctly using map : 或更简洁地使用map

 GetElementValue(){
    var t = (<HTMLInputElement> this.inputs.map(t=>t.nativeElement)[1]).value;
    console.log(t);
 }

For more information, here is everything you need to know about how to use @ViewChildren: 有关更多信息,这是有关如何使用@ViewChildren的所有信息:

https://netbasal.com/understanding-viewchildren-contentchildren-and-querylist-in-angular-896b0c689f6e https://netbasal.com/understanding-viewchildren-contentchildren-and-querylist-in-angular-896b0c689f6e

https://blog.angular-university.io/angular-viewchild/ https://blog.angular-university.io/angular-viewchild/

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

相关问题 无法使用 document.getelementById(id) 将焦点设置在 angular 编辑器上 - Unable to set focus on angular editor using document.getelementById(id) 使用 angular 7 中的 getElementById 在 DOM 元素上应用指令 - Apply a directive on a DOM element using getElementById in angular 7 Angular document.getElementById 不能动态使用插值 - Angular document.getElementById not working with interpolation dynamically angular4 / typescript中的document.getElementById替换? - document.getElementById replacement in angular4 / typescript? 替换 Angular 中的 document.getElementById('checkBox') - Replacing document.getElementById('checkBox') in Angular 遍历内部DOM元素并使用angular 2+替换内部DOM - Traverse through inner DOM element and replace inner DOM using angular 2+ 如何使用 *ngFor 中的动态键获取数组:Angular 2+ - How to get the array using a dynamic key in *ngFor : Angular 2+ 动画单个元素 ngfor angular 而不使用变量 - Animate a single element ngfor angular without using a variable Angular 测试 - 如何在 angular 测试中模拟 document.getElementbyId? - Angular test - how to mock document.getElementbyId in angular test? Angular:如何在没有事件的情况下获取 *ngFor DOM 元素后面的对象? - Angular: How to get object behind *ngFor DOM element without an event?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM