Skip to content

Instantly share code, notes, and snippets.

@srph
Created November 21, 2014 00:11
Show Gist options
  • Select an option

  • Save srph/ef4e6fda237f334b84f2 to your computer and use it in GitHub Desktop.

Select an option

Save srph/ef4e6fda237f334b84f2 to your computer and use it in GitHub Desktop.
[AngularJS] basic countdown. angular version of (https://gist.github.com/srph/fcd27d3e87a483b26ee9)
'use strict';
angular
.module('app')
.directive('CountdownControl', CountdownControl);
function CountdownControl() {
var template = [
'<span ng-click="controlCtrl.toggleTick()">',
'{{ controlCtrl.state }}',
'</span>'
].join('');
return {
scope: {
flag: '=',
resume: '&',
pause: '&'
}
require: '^CountdownTimer'
template: template,
controller: controller,
controllerAs: 'controlCtrl'
}
function controller($scope) {
var vm = this;
vm.state = vm.flag ? 'Pause' : 'Resume';
vm.toggleTick = toggleTick;
function toggleTick() {
if ( angular.isUndefined(vm.flag) ) {
vm.pause();
} else {
vm.resume();
}
}
}
}
'use strict';
angular
.module('app')
.directive('CountdownTimer', CountdownTimer);
function CountdownTimer() {
/** Directive template */
var template = [
'<countdown-control
flag="timerCtrl.interval"
pause="timerCtrl.setInterval()"
resume="timerCtrl.clearInterval()">',
'</countdown>',
'{{ timerCtrl.elapsed }} seconds has passed.'
].join('');
// ------------
//
return {
template: getTemplate,
link: linkFn,
controller: controller,
controllerAs: 'timerCtrl'
}
/** Start the countdown */
function linkFn(scope, element, attributes, controller) {
controller.setInterval();
}
/** Just a retarded API */
function controller($scope) {
var vm = this;
vm.elapsed = 0;
vm.clearInterval = clearInterval
vm.setInterval = setInterval;
vm.tick = tick;
function setInterval() {
vm.interval = setInterval.call(null, vm.tick);
}
function clearInterval() {
vm.interval = clearInterval(vm.interval);
}
function tick() {
vm.elapsed += 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment