Khaled Ramadan
Khaled Ramadan

Reputation: 832

How to count the ion-select Childs (ion-option)?

It possible to count the ion-option inside the ion-select?

<ion-select>
  <ion-option></ion-option>
  <ion-option></ion-option>
  <ion-option></ion-option>
</ion-select>

console.log(something) and I will got 3 ?

Thanks.

Upvotes: 2

Views: 538

Answers (2)

carton
carton

Reputation: 1168

[EDIT] Sorry for my last answer which was wrong.

You can also use Viewchild to access to the dom element if you don't want to use document.getElementById('mySelect')

So the solution with ViewChild

myPage.html

 <ion-select #mySelect>
     <ion-option>Bacon</ion-option>
    <ion-option>Black Olives</ion-option>
    <ion-option>Extra Cheese</ion-option>
    <ion-option>Mushrooms</ion-option>
    <ion-option>Pepperoni</ion-option>
    <ion-option>Sausage</ion-option>
 </ion-select>

First use it in your components:

import { Component,ViewChild } from '@angular/core';

Then Declare your variable:

@ViewChild('mySelect') selectDom;

 ionViewDidLoad(){
      console.log(this.selectDom._options.length); // = 6 in my case
  }

Upvotes: 2

ForestG
ForestG

Reputation: 18105

Something like

 @ViewChildren('ion-option')
  ionOptions: QueryList<any>;

and then

ionOptions.length;

Upvotes: 0

Related Questions