Curriculum
Course: AngularJS
Login

Curriculum

AngularJS

AngularJS Tutorial

0/65
Text lesson

The $interval Service

The $interval service is AngularJS’s equivalent of the window.setInterval function.

Example

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);
});

Create Your Own Service

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);
  }
});

Example

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);
});