简体   繁体   English

angular 4 如何访问ViewChildren_results

[英]angular 4 how to access ViewChildren _results

i have a list of checkboxes inside an ngFor:我在 ngFor 中有一个复选框列表:

    <md-checkbox
      #hangcheck
      [id]="hangout?.$key"
      class="mychecks"
      >
    I'm Interested
  </md-checkbox> 

i refrence them in the component like so:我像这样在组件中引用它们:

@ViewChildren("hangcheck") hangchecks: QueryList<any>;

then in ngAfterViewInit i need to loop them:然后在 ngAfterViewInit 中我需要循环它们:

  ngAfterViewInit(){
console.log('the array: ',this.hangchecks)
this.hangchecks._results.forEach((item) => {
  console.log('the item: ',item)
});
 }

but i get: Property '_results' is private and only accessible within class 'QueryList' in the console i see this:但我得到:属性 '_results' 是私有的,只能在控制台的 class 'QueryList' 内访问 我看到这个: 在此处输入图像描述 so as you can see there is the array in the _results.如您所见,_results 中有数组。 but how can i access it and loop it?但是我怎样才能访问它并循环播放它呢?

调用toArray()方法:

this.hangchecks.toArray().forEach((item) => {

要访问元素,您必须等到它准备好

this.hangchecks.changes.subscribe(a => a.forEach((b, i) => console.log(b)));

To access ith element in _results in the queryList, call toArray() method:要访问 queryList 中 _results 中的第 i 个元素,请调用toArray()方法:

this.queryListObj.toArray().[i]

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

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