简体   繁体   English

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

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

I am new to ionic 2 / angular 2. 我是离子2 /角2的新手。
I want to display a operators select box exactly when the user complete the 10 digit mobile number input. 我想在用户完成10位手机号码输入时准确显示一个操作员选择框。 Till now i have created a function in ts 到目前为止,我已经在ts中创建了一个函数

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

and calling this function as 并调用此函数为

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

and the div to display is 而要显示的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>  

it shows the desired functionality. 它显示了所需的功能。 But i want to know is i am using the right way or is there any other good solution for this. 但是我想知道是我使用的是正确的方法,还是有其他好的解决方案。

You can avoid having any code-behind by doing this: 您可以通过执行以下操作避免任何代码隐藏:

  <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>

Assuming less code means simplicity, this might be better. 假设更少的代码意味着简单,那可能会更好。

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

相关问题 精确匹配边界未知的10位数字? - Match exactly 10 digits with unknown boundaries? 如何在离子移动设备的html中输入10位数字时突出显示键盘按钮 - How to highlight a keyboard button when 10 digits are typed in html in ionic mobile AngularJS中文本框更改时调用延迟函数 - Call Function with Delay When Textbox Changes in AngularJS 编辑文本框时如何调用jQuery函数 - How to call a jQuery function when textbox is edited 如何验证一个单元格在spreadjs中是否正好有10位数字? - How to validate a whether a cell has exactly 10 digits in spreadjs? 在文本框中超过最大位数后显示离子弹出 - Show Ionic Popup after exceeding maximum number of digits in a textbox 将字符插入输入文本框中时的制表符光标 - Tab cursor when character inserted into input textbox 当文本框不为null /一些值传递到文本框时,如何从文本框调用javascript函数 - How to call a javascript function from a textbox when the textbox is not null / some value is passed into the textbox 当用户离开javascript中的文本框时如何调用函数 - How to call a function when user leaves a textbox in javascript 当javascript填充文本框中的值时调用onTextChanged函数 - Call onTextChanged function when javascript fills value in textbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM