扫码关注官方订阅号
求大神:angularJS中倒计时60秒的按钮如何写?
人生最曼妙的风景,竟是内心的淡定与从容!
我写的略多一点,plunker,是一个directive
directive
index.html
<!DOCTYPE html> <html ng-app="demo"> <head> <script data-require="angular.js@1.4.8" data-semver="1.4.8" src="https://code.angularjs.org/1.4.8/angular.js"></script> <link rel="stylesheet" href="style.css" /> </head> <body ng-controller="DemoCtrl"> <h1>Hello Plunker!</h1> <timerbutton show-timer="true" timeout="time">Hello There</timerbutton> <script src="script.js"></script> </body> </html>
script.js
// Code goes here var demo = angular.module('demo', []); demo.controller('DemoCtrl', function($scope){ $scope.time = 6000; }); demo.directive('timerbutton', function($timeout, $interval){ return { restrict: 'AE', transclude: true, scope: { showTimer: '=', onClick: '&', timeout: '=' }, link: function(scope, element, attrs){ scope.timer = true; scope.timerCount = scope.timeout; var counter = $interval(function(){ scope.timerCount = scope.timerCount - 1000; }, 1000); $timeout(function(){ scope.timer = false; $interval.cancel(counter); scope.showTimer = false; }, scope.timeout); }, template: '<button ng-click="onClick()" ng-disabled="timer"><span ng-transclude></span> <span ng-if="showTimer">({{ timerCount / 1000 }}s)</span></button>' }; });
angular.module('myapp', []) .controller('demoController', ['$scope', '$interval', function($s, $i) { $s.time = 60; var timer = null; timer = $i(function(){ $s.time = $s.time - 1; if($s.time === 0) { $i.cancel(timer); } }, 1000); }]);
写一个 directive(指令), 把1楼的倒计时封装上去, 如果要加回调用指令 isolate scope 的 & 传递函数名称。
$interval有循环多少次的功能$interval(function (count){
//循环60次
} , 1000 , 60);
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
我写的略多一点,plunker,是一个
directive
index.html
script.js
写一个 directive(指令), 把1楼的倒计时封装上去, 如果要加回调用指令 isolate scope 的 & 传递函数名称。
$interval有循环多少次的功能
$interval(function (count){
} , 1000 , 60);