Skip to content

AngularJS: Models and services

Reference: AngularJS in Action

Services are a way to define functionality that's common to the entire application.

There are five types of services that you can define using the module convenience methods: module.constant, module.value, module.service, module.factory, and module.provider.

Communication with remote servers is facilitated with the $http service, which has shortcut methods built into it that mirror REST verbs.

The $http service is based on a deferred / promise API that provides mechanisms for you to handle asynchronous calls to the server.

Further control over asynchronous operations can be exerted (运用、发挥、施以影响) by using a deferred object returned by calling $q.deferred.

Calls made using $http can be intercepted by creating an interceptor ad pushing it into the $httpProvider.interceptors array during the module.config block.

Services can be enhanced by capturing them in a $provide.decorator call in the module.config block and modifying its behavior.

$httpBackend is great for mocking out server-side calls and defining the response.

Manually resolve a promise in a unit test by calling $rootScope.$digest.

天上的神明与星辰,人间的艺术和真纯,我们所敬畏和热爱的,莫过于此。