The $interval service is AngularJS’s equivalent of the window.setInterval function.
Show the current time every second.
var app = angular.module(‘myApp’, []); app.controller(‘myCtrl’, function($scope, $interval) { $scope.theTime = new Date().toLocaleTimeString(); $interval(function () { $scope.theTime = new Date().toLocaleTimeString(); }, 1000); }); |
To create a custom service, link it to your module and define it as hexafy.
app.service(‘hexafy’, function() { this.myFunc = function (x) { return x.toString(16); } }); |
Utilize the custom service named hexafy to convert a number into its hexadecimal equivalent.
app.controller(‘myCtrl’, function($scope, hexafy) { $scope.hex = hexafy.myFunc(255); }); |