2017-01-25 40 views
0

我想選擇元素列表,以便在給定值的選項時將其中的一個關注。無法使用@ViewChildren獲取* ngFor循環的<md-option>元素

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 
} 

回答

2

你能想到把認購超過QueryListselectBox

this.selects.changes.subscribe((option: QueryList <any>) => { 
    console.log(option); 
}); 
+0

就是這樣的。謝謝。最後一個問題:QueryList類型爲'',是嗎? – kyw

+1

@kyw在這裏是的,當涉及到'component'時,它會組件'Type' –