Beginning AngularJS

(WallPaper) #1

Chapter 7 ■ ServiCeS and Server CommuniCation



The URL is: {{url}}



  • {{protocol}}

  • {{host}}

  • {{port}}




As we did with the $window service, we simply asked for the $location service by adding it as a parameter to our
controller’s anonymous function. In the next chapter, we look at how better to organize our HTML views, and you will
see how the $location service plays a very important role in this context.


The $document Service

We will finish up this section with a brief look at the $document service. This service is essentially a jqLite (or jQuery)
wrapper for the browser’s window.document object. Examine Listing 7-3.


Listing 7-3. Using the $document Service to Access the Page Title


<!DOCTYPE html >




Angular Services




The page title is: {{docTitle}}




Yet again, we ask for this service by specifying it as a parameter on our controller method’s anonymous function.
Next, we use it to get the value of the page’s title element, and we set a property on the $scope, docTitle, on this value.
Angular generally discourages accessing the DOM directly, as it operates very much on the principle that a
declarative approach is much better. There are times when you need this direct access, however, so it is good to have
services such as this one available.

Free download pdf