简体   繁体   English

如何从 QueryList 的元素中获取 innerText<t> ?</t>

[英]How to get innerText from an element of QueryList<T>?

addGoatToGroupForVaccination(goatObject: {EarTag:string, GoatId:number}){

for (let i = 0; i < this.goatsGroupForVaccination.length; i++){
  if (this.goatsGroupForVaccination[i].EarTag === goatObject.EarTag){
    this.goatsGroupForVaccination.splice(i, 1);
    for (let i = 0; i < this.vaccinateGoatButtons.toArray().length; i++){
      if (this.vaccinateGoatButtons[i].nativeElement.innerText === goatObject.EarTag){
        this.vaccinateGoatButtons[i].remove;
      }
    }
  }
}

this.goatsGroupForVaccination.push(goatObject);

} }

I get a "Cannot read property nativeElement of undefined. All I want is to get the innerText value of a button. How can this be done?我得到一个“无法读取未定义的属性 nativeElement。我想要的只是获取按钮的 innerText 值。如何做到这一点?

You can get undefined for two reasons (I cannot tell you why because there is not enough code in your question).您可能会因为两个原因而undefined (我无法告诉您原因,因为您的问题中没有足够的代码)。

Maybe you are accessing those elements before the elements are fully initialized by using the wrong Angular lifecycle hook.也许您在使用错误的 Angular 生命周期挂钩完全初始化元素之前访问这些元素。 For example, if we use @ContentChildren() :例如,如果我们使用@ContentChildren()

  @ContentChildren(XComponent) xComponents: QueryList<XComponent>;

  ngOnInit() {
    // here this.xComponents === undefined.
  }


  ngAfterContentInit() {
    console.log(this.xComponents) // <-- take a look if you have the nativeElement prop before trying to access it 
    this.xComponents.forEach((xComponent: XComponent) => {
      // do something with each xComponent
    })
  }

If you have the nativeElement property in your console.log, then you can just use it and the problem is solved.如果你的 console.log 中有nativeElement属性,那么你可以直接使用它,问题就解决了。 But if you do not have the nativeElement property in your QueryList<T> , just like in this example:但是,如果您的QueryList<T>中没有nativeElement属性,就像在此示例中一样:

没有 ElementRef 的示例

Then you can inject the ElementRef in the constructor of the children components as public (just be warned that this might represent a security issue, potentially exposing your app to XXS attacks. I am telling this because your App might be internal or you could not care about a security vulnerability).然后您可以将ElementRef作为公共注入子组件的构造函数(请注意,这可能代表安全问题,可能会使您的应用程序遭受 XXS 攻击。我这么说是因为您的应用程序可能是内部的,或者您不在乎关于安全漏洞)。

This is how to inject the elementRef as public in the constructor of the child component:这是如何在子组件的构造函数elementRef作为 public 注入:

  constructor(public elementRef: ElementRef) {}

Then you will have the ElementRef with the nativeElement property nested inside:然后,您将拥有嵌套在内部的带有nativeElement属性的 ElementRef:

ElementRef 示例

Again, this might expose you to a web security issue (if you are interested in reading more about security, take a look at the Angular security guide )同样,这可能会使您面临 web 安全问题(如果您有兴趣阅读有关安全的更多信息,请查看Angular 安全指南

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

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