首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动接收OTP并验证OTP,UI不更新

自动接收OTP并验证OTP,UI不更新
EN

Stack Overflow用户
提问于 2018-08-09 12:17:15
回答 2查看 1.3K关注 0票数 1

我正试图为我的Ionic 4安卓应用程序实现自动otp验证。我在下面的代码中尝试过,我能够接收到消息,但是UI输入字段没有更新接收到的OTP

app.component.ts

代码语言:javascript
复制
      constructor(public alertCtrl: AlertController,
    public platform:Platform,
    public androidPermissions: AndroidPermissions,
    public http:Http,
  public navCtrl:NavController,
  public navParams: NavParams) {
    document.addEventListener('onSMSArrive', function(e){
      var sms = e.data;

     console.log("received sms "+JSON.stringify( sms ) );

     if(sms.address=='HP-611773') //look for your message address
     {
       this.otp=sms.body.substr(0,4);

      this.verify_otp();
     }
    });

      }


 verifyOTP()
    {
      console.log("verify otp");

    }

我能够使用OTP看到警报,但下面的UI没有更新。

app.component.html

代码语言:javascript
复制
<ion-header>
  <ion-toolbar>
    <ion-button size="small" (click)="goBackToFirstTimeLogin()">Back</ion-button>
    <ion-title>verifyOTP</ion-title>
  </ion-toolbar>
</ion-header>
<ion-content padding>
  <ion-item>
    <ion-label floating>Enter OTP</ion-label>
    <ion-input type="text" [(ngModel)]="otp"></ion-input>
  </ion-item>
  <button ion-button (click)="verifyOTP()">Verify</button>
  <ion-button size="small" (click)="setPassword()">SetUp Password</ion-button>
</ion-content>

`

[(ngModel)]="otp"值没有更新。

我正在犯以下错误:

犯了这样的错误

我遵循下面的GitHub链接:

https://github.com/bharathirajatut/ionic3/tree/master/SMSOTPVerificationAutomate

你能帮我一个人吗,谢谢提前!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-17 17:54:17

在我阅读了很多文档之后,我得到了这样的解决方案:代码不是角码,代码是非角的,所以角不知道更新视图,所以UI不更新。

因此,非角码可以使用Zone.js进行更新。

在下面的代码中,我用于更新代码:

代码语言:javascript
复制
 this.zone.run(() => {
          this.otp = sms.body.substr(20, 4);
          this.stopSMS();
        });

整个代码下面的 :

代码语言:javascript
复制
 document.addEventListener('onSMSArrive', function(e){
      var sms = e.data;

     console.log("received sms "+JSON.stringify( sms ) );

     if(sms.address=='HP-611773') //look for your message address
     {
        this.zone.run(() => {
          this.otp = sms.body.substr(20, 4);
          this.verifyOTP();
        });
     }
    });

现在UI更新得非常好。

票数 0
EN

Stack Overflow用户

发布于 2018-08-14 18:08:10

将其放入ionViewCanEnter()中,以便在每次加载视图时对其进行更新

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51766821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档