16.07.2017 Views

AngularJS Essentials

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Dependency Injection and Services<br />

In order to configure our provider, we need to use the config function of the<br />

Module API, injecting the service through its function. In the following code, we are<br />

calling the setParkingRate method of the provider, overwriting the default rate<br />

that comes from the parkingConfig method.<br />

config.js<br />

parking.config(function (parkingServiceProvider) {<br />

parkingServiceProvider.setParkingRate(10);<br />

});<br />

The other service components such as constants, values, factories, and services are<br />

implemented on the top of the provider component, offering developers a simpler<br />

way of interaction.<br />

Using <strong>AngularJS</strong> built-in services<br />

Now, it's time to check out the most important and useful built-in services for<br />

our daily development. In the following topics, we will explore how to perform<br />

communication with the backend, create a logging mechanism, support timeout,<br />

single-page application, and many other important tasks.<br />

Communicating with the backend<br />

Every client-side JavaScript application needs to communicate with the backend. In<br />

general, this communication is performed through an interface, which is exposed by<br />

the server-side application that relies on the HTTP protocol to transfer data through<br />

the JSON.<br />

HTTP, REST, and JSON<br />

In the past, for many years, the most common way to interact with the backend was<br />

through HTTP with the help of the GET and POST methods. The GET method was<br />

usually used to retrieve data, while POST was used to create and update the same<br />

data. However, there was no rule, and we were feeling the lack of a good standard<br />

to embrace.<br />

The following are some examples of this concept:<br />

GET /retrieveCars HTTP/1.1<br />

GET /getCars HTTP/1.1<br />

GET /listCars HTTP/1.1<br />

GET /giveMeTheCars HTTP/1.1<br />

GET /allCars HTTP/1.1<br />

[ 76 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!