前端学习——ionic/AngularJs——获取验证码倒计时按钮
按钮功能为:点击“获取验证码”——按钮不可用-设置倒计时-60秒后重新获取。
代码借鉴于:http://plnkr.co/edit/Swj82MpJSix3a47jZRHP?p=preview
主要实现原理:点击后,设置一个$interval,每一秒更改一次剩余时间,并依赖Angular数据绑定实时显示在页面中。设置一个$timeout,60秒后将按钮初始化到可用状态。
实现代码:
(1)js代码,设置成一个directive以便多次调用。
angular.module('winwin').directive('timerbutton', function($timeout, $interval){
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"><span ng-if="showTimer">{{ timerCount }}</span>{{text}}</button>'
};
});
(2)html代码
<timerbutton show-timer="false">获取验证码</timerbutton>
实现效果:
(1)点击之前

(2)点击之后

干货:
<html ng-app="myApp">
<head>
<title>测试_短信验证码</title>
<meta charset="utf-8" />
</head>
<script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css"/>
<body>
<timerbutton show-timer="false">获取验证码</timerbutton>
</body>
</html>
<script>
angular.module('myApp',[]).directive('timerbutton', function($timeout, $interval){
return {
restrict: 'AE',
scope: {
showTimer: '=',
timeout: '='
},
link: function(scope, element, attrs){
scope.timer = false;
scope.timeout = 60000;
console.log("1-进来了");
scope.timerCount = scope.timeout / 1000;
scope.text = "获取验证码"; scope.onClick = function(){
console.log("2-进来了");
scope.showTimer = true;
scope.timer = true;
scope.text = "秒后重新获取";
var counter = $interval(function(){
scope.timerCount = scope.timerCount - 1;
console.log("3-进来了");
}, 1000); $timeout(function(){
console.log("进来了");
scope.text = "获取验证码";
scope.timer = false;
$interval.cancel(counter);
scope.showTimer = false;
scope.timerCount = scope.timeout / 1000;
}, scope.timeout);
}
},
template: '<button ng-click="onClick()" class="btn btn-primary" ng-disabled="timer"><span ng-if="showTimer">{{ timerCount }}</span>{{text}}</button>'
};
});
</script>
前端学习——ionic/AngularJs——获取验证码倒计时按钮的更多相关文章
- Android中注册获取验证码倒计时按钮
public class CountDownTimerUtils extends CountDownTimer { private TextView mTextView; /** * @param t ...
- Andorid实现点击获取验证码倒计时效果
这篇文章主要介绍了Andorid实现点击获取验证码倒计时效果,这种效果大家经常遇到,想知道如何实现的,请阅读本文 我们在开发中经常用到倒计时的功能,比如发送验证码后,倒计时60s再进行验证码的获取 ...
- iOS 短信验证码倒计时按钮的实现
验证码倒计时按钮的应用是非常普遍的,本文介绍了IOS实现验证码倒计时功能,点击获取验证码,进入时间倒计时,感兴趣的小伙伴们可以参考一下: 实现思路: 创建按钮,添加点击方法: 用NSTimer定时器, ...
- Android 获取验证码倒计时实现
Android 获取验证码倒计时实现 2017年10月24日 09:55:41 FBY展菲 阅读数:2002 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- angular中service封装$http做权限时拦截403等状态及获取验证码倒计时、跨域问题解决
封装$http.做权限时拦截403等状态及获取验证码倒计时: 拦截接口返回状态 var app = angular.module('app'); app.factory('UserIntercepto ...
- clips 前端 js 倒计时 获取验证码的按钮
<a href="javascript:void(0);" onclick="get_captcha()" class="btn btn-def ...
- js点击按钮获取验证码倒计时
//发送验证码倒计时 var clock = ''; var nums = 60; var btn; $("#btnGetVerCode").click(function () { ...
- android学习小例子——验证码倒计时按钮
1.activity_main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro ...
- iOS开发之--获取验证码倒计时及闪烁问题解决方案
大家在做验证码的时候一般都会用到倒计时,基本上大家实现的方式都差不多,先贴出一些代码来.. -(void)startTime{ __block ; //倒计时时间 dispatch_queue_t q ...
随机推荐
- 如何有效的遍历django的QuerySet
最近做了一个小的需求,在django模型中通过前台页面的表单的提交(post),后台对post的参数进行解析,通过models模型查询MySQL,将数据结构进行加工,返回到前台页面进行展示.由于对dj ...
- SharpDevelop插件开发手册
SharpDevelop插件开发手册部分内容摘取自:http://www.cnblogs.com/CBuilder的SharpDevelop开发教程 SharpDevelop插件开发手册 第一章 ...
- jQuery手机对话框插件
最近,公司一直在做微网站之类的,一直在看别的微网站,发现一些对话框的样式很不错,所以自己就动手把样式剥离出来写成一个简单的插件,方便其他项目中使用到.废话不多说,上插件源码: /* *jQuery简单 ...
- 将网站固定到开始菜单,自定义图标、颜色和Windows推送通知
Windows 8.1——将网站固定到开始菜单,自定义图标.颜色和Windows推送通知 记得在IE 9和Windows 7刚出来那会儿我写过一篇文章来介绍如何自定义网站将其固定到Windows的任务 ...
- Power Designer导出实体类和NHibernate xml文件
Power Designer导出实体类和NHibernate xml文件 今天研究了一下通过PowerDesigner生成实体类和NHibernate所需要的xml文件,方法是通过Power Desi ...
- C#单例模式的三种写法 以及 继承面试题
1.没有考虑线程安全 public class Singleton { private static Singleton _instance = null; private Singleton(){} ...
- 讲讲Linq to SQL映射(基础篇)
讲讲Linq to SQL映射(基础篇) 这篇主要讲Linq to SQL基于属性的映射.即映射数据库,映射表,映射列,映射关系,映射存储过程, 映射函数.然而创建这种映射有三种方法,他们分别是OR ...
- requestScope含义
requestScope表名一个http请求的整个生命周期,它只是一个定义而已,不是一个对象. ${requestScope.info}就等价于request.getAttribute("i ...
- Centos6.3不能使用yum install安装gcc编辑器解决办法
mv /var/lib/rpm/__db* /tmp rpm --rebuilddb yum clean all
- Python之路2Day(基础深入)
一,整数 1.int型 具有的功能: class int(object): (1).返回表示该数字的时占用的最少位数 def bit_length(self): 例: >>> bin ...