O'Reilly logo

Learning AngularJS by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing Simple Time Service

In this example you will build a simple time service that generates a local time object for different cities. Then in the AngularJS template you will use the time service in multiple controllers. The purpose is to give you a chance to see how easy it is to reuse an AngularJS service.

The code in Listing 10.3 implements a custom service named TimeService using the function TimeObj() to generate the service object. The code in TimeObj simply defines a list of cities with a time zone offset and provides the getTZDate() function to return a JavaScript Date object for a specific city. The getCities() function creates an array of the cities represented and returns it.

Notice that there are several controllers added ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required