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

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

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

&lt;div class=&quot;my-loader&quot; *ngIf=&quot;loader; else show_form_content&quot;&gt;
    &lt;ngx-loading [show]=&quot;loader&quot;&gt;&lt;/ngx-loading&gt;
  &lt;/div&gt;
  &lt;ng-template #show_form_content&gt;
   &lt;div class=&quot;gifTimer&quot; id=&quot;gif-html&quot;&gt;
 
   &lt;/div&gt;
  &lt;/ng-template&gt;
 
 &lt;/div&gt;

 getMyData() {
  this.loader = true;
  this.myService
    .getData()
    .subscribe((response) =&gt; {
      this.loader = false;
      if (response.status === &#39;success&#39;) {
        let gifHtml = document.getElementById(&#39;gif-html&#39;);
        console.log(&#39;gifHtml&#39;, gifHtml)
      }
    });
}

ngOnInit(){
  this.getMyData()
}

答案1

得分: 1

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

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

HTML

<div class="my-loader" *ngIf="loader; else show_form_content">
  <ngx-loading [show]="loader"></ngx-loading>
</div>

<ng-template #show_form_content>
  <div class="gifTimer" id="gif-html">
  </div>
</ng-template>

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

import { ViewChild, ElementRef, AfterViewInit } from '@angular/core';

export class YourComponent {
  @ViewChild('gif-html', { static: false }) gifHtmlRef!: ElementRef;

  ngAfterViewInit() {
    this.getMyData();
  }

  getMyData() {
    this.loader = true;
    this.myService
      .getData()
      .subscribe((response) => {
        this.loader = false;
        if (response.status === 'success') {
          console.log('gifHtml', this.gifHtmlRef.nativeElement);
        }
      });
  }
}

我希望这对你有所帮助。

英文:

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

&lt;div class=&quot;my-loader&quot; *ngIf=&quot;loader; else show_form_content&quot;&gt;
  &lt;ngx-loading [show]=&quot;loader&quot;&gt;&lt;/ngx-loading&gt;
&lt;/div&gt;

&lt;ng-template #show_form_content&gt;
  &lt;div class=&quot;gifTimer&quot; id=&quot;gif-html&quot;&gt;
 &lt;/div&gt;
&lt;/ng-template&gt;

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

import { ViewChild, ElementRef, AfterViewInit  } from &#39;@angular/core&#39;;


export class YourComponent {
  @ViewChild(&#39;gif-html&#39;, { static: false }) gifHtmlRef!: ElementRef;

	ngAfterViewInit() {
	  this.getMyData();
	}

	getMyData() {
		this.loader = true;
		this.myService
		  .getData()
		  .subscribe((response) =&gt; {
			this.loader = false;
			if (response.status === &#39;success&#39;) {
			  console.log(&#39;gifHtml&#39;, this.gifHtmlRef.nativeElement);
			}
		});
	}
}

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:

确定