访问<ng-template>元素的方法是什么?

huangapple go评论106阅读模式
英文:

How to access element of <ng-template>

问题

我正在尝试通过ID访问位于<ng-template>内部的元素,并在加载器为false时显示<ng-template>。因此,在订阅函数中,在加载器变为false后,我的<ng-template>被渲染,在订阅方法本身中,我试图访问我的'gif-html',但我得到了null。

英文:

I am trying to access element by id which is inside the &lt;ng-template&gt; and I am showing that &lt;ng-template&gt; when loader is false. So in subscribe function after loader becomes false my <ng-template> is renedering, and in subscribe method itself I am trying to access my 'gif-html' but I am getting it null

  1. &lt;div class=&quot;my-loader&quot; *ngIf=&quot;loader; else show_form_content&quot;&gt;
  2. &lt;ngx-loading [show]=&quot;loader&quot;&gt;&lt;/ngx-loading&gt;
  3. &lt;/div&gt;
  4. &lt;ng-template #show_form_content&gt;
  5. &lt;div class=&quot;gifTimer&quot; id=&quot;gif-html&quot;&gt;
  6. &lt;/div&gt;
  7. &lt;/ng-template&gt;
  8. &lt;/div&gt;
  9. getMyData() {
  10. this.loader = true;
  11. this.myService
  12. .getData()
  13. .subscribe((response) =&gt; {
  14. this.loader = false;
  15. if (response.status === &#39;success&#39;) {
  16. let gifHtml = document.getElementById(&#39;gif-html&#39;);
  17. console.log(&#39;gifHtml&#39;, gifHtml)
  18. }
  19. });
  20. }
  21. ngOnInit(){
  22. this.getMyData()
  23. }

答案1

得分: 1

getElementById方法返回null是因为当你尝试访问时,ng-template的内容尚未在DOM中呈现。

ng-template是一个结构性指令,它不会立即呈现其内容。相反,它作为一个模板,可以通过ngIf或ngTemplateOutlet指令进行条件性渲染。

HTML

  1. <div class="my-loader" *ngIf="loader; else show_form_content">
  2. <ngx-loading [show]="loader"></ngx-loading>
  3. </div>
  4. <ng-template #show_form_content>
  5. <div class="gifTimer" id="gif-html">
  6. </div>
  7. </ng-template>

我将id="gif-html"添加到了div元素中作为模板引用变量。

  1. import { ViewChild, ElementRef, AfterViewInit } from '@angular/core';
  2. export class YourComponent {
  3. @ViewChild('gif-html', { static: false }) gifHtmlRef!: ElementRef;
  4. ngAfterViewInit() {
  5. this.getMyData();
  6. }
  7. getMyData() {
  8. this.loader = true;
  9. this.myService
  10. .getData()
  11. .subscribe((response) => {
  12. this.loader = false;
  13. if (response.status === 'success') {
  14. console.log('gifHtml', this.gifHtmlRef.nativeElement);
  15. }
  16. });
  17. }
  18. }

我希望这对你有所帮助。

英文:

getElementById method is returning null because the ng-template content is not rendered in the DOM when you're trying to access it.

The ng-template is a structural directive that doesn't render its content immediately. Instead, it serves as a template that can be rendered conditionally using the *ngIf or *ngTemplateOutlet directives.

HTML

  1. &lt;div class=&quot;my-loader&quot; *ngIf=&quot;loader; else show_form_content&quot;&gt;
  2. &lt;ngx-loading [show]=&quot;loader&quot;&gt;&lt;/ngx-loading&gt;
  3. &lt;/div&gt;
  4. &lt;ng-template #show_form_content&gt;
  5. &lt;div class=&quot;gifTimer&quot; id=&quot;gif-html&quot;&gt;
  6. &lt;/div&gt;
  7. &lt;/ng-template&gt;

I added the id="gif-html" template reference variable to the div element.

  1. import { ViewChild, ElementRef, AfterViewInit } from &#39;@angular/core&#39;;
  2. export class YourComponent {
  3. @ViewChild(&#39;gif-html&#39;, { static: false }) gifHtmlRef!: ElementRef;
  4. ngAfterViewInit() {
  5. this.getMyData();
  6. }
  7. getMyData() {
  8. this.loader = true;
  9. this.myService
  10. .getData()
  11. .subscribe((response) =&gt; {
  12. this.loader = false;
  13. if (response.status === &#39;success&#39;) {
  14. console.log(&#39;gifHtml&#39;, this.gifHtmlRef.nativeElement);
  15. }
  16. });
  17. }
  18. }

i hope it's help

huangapple
  • 本文由 发表于 2023年5月11日 19:19:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定