Created
November 9, 2013 10:43
-
-
Save heluvaguy/7384190 to your computer and use it in GitHub Desktop.
Create Clock Using AngularJs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myApp = angular.module('myApp', []); | |
myApp.directive('clock', function($timeout, dateFilter){ | |
return function(scope, element, attrs){ | |
var timeoutId; // timeoutId, so that we can cancel the time updates | |
// schedule update in one second | |
function updateLater() { | |
// save the timeoutId for canceling | |
timeoutId = $timeout(function() { | |
element.text(dateFilter(new Date(), 'yyyy/MM/dd HH:mm:ss ')); | |
updateLater(); // schedule another update | |
}, 1000); | |
} | |
// listen on DOM destroy (removal) event, and cancel the next UI update | |
// to prevent updating time ofter the DOM element was removed. | |
element.bind('$destroy', function() { | |
$timeout.cancel(timeoutId); | |
}); | |
updateLater(); // kick off the UI update process. | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment