首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular7,叫ElementRef来

Angular7,叫ElementRef来
EN

Stack Overflow用户
提问于 2020-02-19 15:35:56
回答 3查看 196关注 0票数 0

我需要在子节点中获取ElementRef。

我的模板

代码语言:javascript
复制
<div #charts *ngFor="let tag of _tags">
  <jqxBulletChart
    style="margin-left: 10px; float: left;"
    [width]='50'
    [height]='350'
    [barSize]='"35%"'
    [labelsFormat]='"c"'
    [title]="''"
    description="{{tag.configuration.name}}"
    [showTooltip]='true'
    [labelsFormatFunction]="labelsFormatFunction"
    [tooltipFormatFunction]="tooltipFormatFunction" [ticks]='ticks'
    [ranges]='tag.value'
    [pointer]='pointer'
    [orientation]="'vertical'"
  >
  </jqxBulletChart>
</div>

我打电话给

@ViewChildren('charts') infoCharts: ElementRef;

得到孩子

代码语言:javascript
复制
for (let chart of this.arrCharts) {
            console.log(chart);
            console.log(chart.nativeElement.lastChild)
            let child: jqxBulletChartComponent = chart.nativeElement.lastChild;
            console.log(child);
            console.log(+this.tags[count].displayValue);
            child.val(+this.tags[count].displayValue || 0);
            // child.val(20);
            count++;
        }

但我的“孩子”应该是ElementRef型的,我不知道怎么做

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-02-19 16:56:19

请尝试下面的代码,它将为您提供ElementRef for jqxBulletChart

代码语言:javascript
复制
@ViewChildren(jqxBulletChartComponent,{read: ElementRef}) hellos: QueryList<ElementRef>;

请注意,您需要提供JQXBulletChartComponent,并且必须将viewChildren读入ElementRef。

如果对你有用的话请告诉我。

编码愉快!

票数 1
EN

Stack Overflow用户

发布于 2020-02-19 16:34:10

我认为它不可能是ElementRef的一个实例,因为您使用的是ngFor,有多个引用,您称之为#图表。它应该是QueryList

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

票数 1
EN

Stack Overflow用户

发布于 2020-02-19 16:57:17

试试这个:

代码语言:javascript
复制
import {AfterViewInit, Component, QueryList, ViewChildren, ElementRef} from '@angular/core';

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"]
})

export class AppComponent {
  name = "Angular";
  @ViewChildren("charts") charts: QueryList<ElementRef>;

  ngAfterViewInit() {
    this.charts.forEach(c => console.log(c));
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60303910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档