无法使用@ViewChildren获取* ngFor循环<md-option>元素

时间:2017-01-25 04:17:49

标签: angular

我想选择元素列表,以便在给出选项值的情况下随意关注其中一个元素。

select.component.template:

<md-select
        [formControl]="selectFormControl"
        (click)="detectKeys()"
        [required]="isRequired"
        [disabled]="isDisabled"
        [attr.role]="'listbox'"
        [attr.aria-labelledby]="placeholder"
        placeholder="placeholder">
  <md-option #selectBox *ngFor="let option of options" [value]="option.value">  
    {{option.description}}
  </md-option>
</md-select>

select.component.ts:

export class UiSelectComponent implements OnInit, AfterViewInit {
    ....
    @Input() options: Object[];
    @ViewChildren('selectBox') selects: QueryList<any>;
    ....
}

ngAfterViewInit() {
   console.log(this.selects.length); // comes up empty results
}

1 个答案:

答案 0 :(得分:2)

您可以考虑将订阅放在QueryList的{​​{1}}

selectBox