简体   繁体   English

querySelector 似乎没有在 ng-container 中找到元素

[英]querySelector does not seem to find elements inside ng-container

I am developing a web application using angular dart.我正在使用 Angular Dart 开发 Web 应用程序。

I am trying to find a 'div' element inside the component using document.querySelector() and I am trying to modify(add some content to) its body.我正在尝试使用 document.querySelector() 在组件内找到一个“div”元素,并且我正在尝试修改(添加一些内容)它的主体。

But it doesn't seem to find the 'div' element.但它似乎没有找到“div”元素。

Here is my html:这是我的 html:

<ng-container *ngFor="let item of list">
  <ng-container *ngIf="item.canShowChart">
    <div [id]="item.elementID" class="chart"></div>
  </ng-container>
</ng-container>

Here is my component method which tries to modify the 'div':这是我尝试修改“div”的组件方法:

  void drawChart() {
    for (final item in list) {
      if (!item.canShowChart) {
        continue;
      }
      final DivElement _container = document.querySelector('#' + item.elementID);
      print(_container);
    }
  }

It always prints the '_container' as 'null'它总是将 '_container' 打印为 'null'

I tried removing the ng-container and having only the 'div' in the page like below and it seems to work..我尝试删除 ng-container 并在页面中只有“div”,如下所示,它似乎有效。

<div [id]="item.elementID" class="chart"></div>

What is the problem?问题是什么?

TIA. TIA。

It is not working because as at the time you used 'querySelectorAll', angular had not loaded ng-container to the DOM yet.它不起作用,因为在您使用“querySelectorAll”时,angular 尚未将 ng-container 加载到 DOM。 You should put your code in the 'AfterViewChecked' lifecycle hook.您应该将代码放在“AfterViewChecked”生命周期挂钩中。

export class ImageModalComponent implements OnInit, AfterViewChecked{
  //AfterViewChecked
  ngAfterViewChecked {
    void drawChart() {
    for (final item in list) {
      if (!item.canShowChart) {
        continue;
      }
      final DivElement _container = document.querySelector('#' + item.elementID);
      print(_container);
    }
  }
 }
}

Make sure to import 'AfterViewChecked' like so;确保像这样导入“AfterViewChecked”;

import { Component, OnInit, AfterViewChecked } from '@angular/core';

You can make it a separate component, let's call it app-chart :您可以将其作为一个单独的组件,我们称它为app-chart

<ng-container *ngFor="let item of list">
  <app-chart *ngIf="item.canShowChart" [item]="item">
  </app-chart>
</ng-container>

In the AppChartComponent declare necessary input(s), and inject ElementRef in the constructor:在 AppChartComponent 中声明必要的输入,并在构造函数中注入 ElementRef:

@Input() item: any;
constructor(private ref: ElementRef) {}

this.ref.nativeElement is how you can access the DOM element from inside. this.ref.nativeElement是您从内部访问 DOM 元素的方式。

Never use querySelector to find elements in your template.切勿使用querySelector在模板中查找元素。 Angular and DOM are two seperate paradigms and you should not mix them. Angular 和 DOM 是两个独立的范例,您不应该混合使用它们。

To find an element in your template, use a reference to an element.要在模板中查找元素,请使用对元素的引用。

<div #chartContainer class="chart"></div>

Then you can reference the div from your code.然后您可以从您的代码中引用div

See https://itnext.io/working-with-angular-5-template-reference-variable-e5aa59fb9af for an explanation.有关解释,请参阅https://itnext.io/working-with-angular-5-template-reference-variable-e5aa59fb9af

AfterViewChecked not worked. AfterViewChecked 无效。 Use AfterViewInit使用 AfterViewInit

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM