JavaScript   发布时间:2022-04-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ionic+AngularJs实现获取验证码倒计时按钮大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

按钮功能为:点击“获取验证码”——按钮不可用-设置倒计时-60秒后重新获取。

主要实现原理:点击后,设置一个$interval,每一秒更改一次剩余时间,并依赖Angular数据绑定实时显示在页面中。设置一个$timeout,60秒后将按钮初始化到可用状态。

实现代码:

(1)js代码,设置成一个directive以便多次调用。

{ return { reStrict: 'AE',scope: { showTimer: '=',timeout: '=' },link: function(scope,element,attrs){ scope.timer = false; scope.timeout = 60000; scope.timerCount = scope.timeout / 1000; scope.text = "获取验证码";
  scope.onClick = function(){
    scope.showTimer = true;
    scope.timer = true;
    scope.text = "秒后重新获取";
    var counter = $interval(function(){
      scope.timerCount = scope.timerCount - 1;
    },1000);

    $timeout(function(){
      scope.text = "获取验证码";
      scope.timer = false;
      $interval.cancel(counter);
      scope.showTimer = false;
      scope.timerCount = scope.timeout / 1000;
    },scope.timeout);
  }
},template: '<button on-tap="onClick()" class="button button-calm xgmm-btn" ng-disabled="timer"&gt;<span ng-if="showTimer"&gt;{{ timerCount }}</span>{{text}}</button>'

};
});

(2)html代码

false">获取验证码

实现效果:

(1)点击之前

ionic+AngularJs实现获取验证码倒计时按钮

(2)点击之后

ionic+AngularJs实现获取验证码倒计时按钮

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持菜鸟教程。

大佬总结

以上是大佬教程为你收集整理的ionic+AngularJs实现获取验证码倒计时按钮全部内容,希望文章能够帮你解决ionic+AngularJs实现获取验证码倒计时按钮所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。