繁体   English   中英

当文本框中恰好插入10位数字时,Ionic 2会调用函数

[英]Ionic 2 call a function when exactly 10 digits inserted in textbox

我是离子2 /角2的新手。
我想在用户完成10位手机号码输入时准确显示一个操作员选择框。 到目前为止,我已经在ts中创建了一个函数

showOperators()
{
    let len = this.mobileNo.length;
    if(len==10)
    {
        this.isOperator = True;
    }
    else
    {
        this.isOperator = False;
    }
} 

并调用此函数为

<ion-input (keypress)="showOperators()" [(ngModel)]="mobileNo"></ion-input>  

而要显示的div是

<div *ngIf="isOperator">
    <ion-select [(ngModel)]="operators">
      <ion-option value="1">Operator1</ion-option>
      <ion-option value="2">Operator2</ion-option>
    </ion-select>
</div>  

它显示了所需的功能。 但是我想知道是我使用的是正确的方法,还是有其他好的解决方案。

您可以通过执行以下操作避免任何代码隐藏:

  <ion-input  [(ngModel)]="mobileNo"></ion-input>  

  <div *ngIf="mobileNo?.length == 10">
    <ion-select>
      <ion-option value="1">Operator1</ion-option>
      <ion-option value="2">Operator2</ion-option>
    </ion-select>

假设更少的代码意味着简单,那可能会更好。

暂无
暂无

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

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