【问题标题】:How to count the ion-select Childs (ion-option)?如何计算离子选择Childs(离子选项)?
【发布时间】:2018-03-20 23:16:04
【问题描述】:

可以在离子选择中计算离子选项吗?

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

console.log(something) 我会得到 3 个?

谢谢。

【问题讨论】:

  • 为什么要数它们?我问这也许是为了找到解决方法

标签: angular ionic2


【解决方案1】:

[编辑] 对不起,我的最后一个答案是错误的。

如果不想使用 document.getElementById('mySelect') 也可以使用Viewchild 访问dom元素

所以使用 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>

首先在你的组件中使用它:

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

然后声明你的变量:

@ViewChild('mySelect') selectDom;

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

【讨论】:

  • 不工作,你能得到一个关于 VIewChild 的例子
  • 我会试着做一个样品给我一分钟
  • 你看所有值显示后的长度吗? (之后 ngFor 被执行)
  • 我认为 ngFor 是有效的,但在我的情况下它不起作用,因为我使用的是异步管道,在加载 viewChild 时计算子节点并返回零。你有解决方案吗
  • 我需要在数组订阅后计算 ion-select Childs
【解决方案2】:

类似

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

然后

ionOptions.length;

【讨论】:

    猜你喜欢
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 2019-03-03
    • 2023-04-06
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多