Home >Web Front-end >JS Tutorial >Developing Angular Apps without a Back End Using MockBackend
This article demonstrates how Angular 2's MockBackend
facilitates independent front-end development, accelerating iteration and testing without back-end dependencies. MockBackend
intercepts HTTP requests, returning predefined responses, streamlining development and minimizing the risk of structural changes.
A ticketing system example illustrates MockBackend
's setup, request handling, and CRUD operation simulation. Angular's dependency injection replaces the default XHRBackend
with MockBackend
, creating a controlled testing environment mimicking server interactions. Transitioning to a production back-end simply involves removing the MockBackend
dependency.
Frequently, front-end and back-end development teams face blocking dependencies. REST APIs, while beneficial, often lead to front-end teams waiting for back-end deliverables. This article presents a solution: creating a mock back-end to enable parallel development and a robust interface that reduces the risk associated with structural changes.
The ticketing system example defines REST endpoints (Table 1, below) and a Ticket
entity (TypeScript class). The complete code and a live preview are available on Plunker (link omitted for brevity).
Table 1: Ticketing System Endpoints
Method | Route | Request body | Description |
---|---|---|---|
GET | /ticket |
None | Request all tickets |
GET | /ticket/:id |
None | Request a single ticket via ID |
POST | /ticket |
Ticket entity | Create or update a ticket |
DELETE | /ticket/:id |
None | Delete a ticket |
The Angular 2 project setup utilizes the Angular 2 Getting Started guide. The index.html
file imports necessary libraries (polyfills, SystemJS, RxJS). system.config.js
configures dependencies. The app/boot.ts
file bootstraps the application.
The boot.ts
file's AppModule
uses Angular's dependency injection to provide MockBackend
and configure the Http
service to use it.
<code class="language-typescript">@NgModule({ providers: [ BaseRequestOptions, MockBackend, { provide: Http, deps: [MockBackend, BaseRequestOptions], useFactory: (backend, options) => { return new Http(backend, options); } } ], // ... rest of the NgModule }) export class AppModule { }</code>
The AppComponent
(using index.html
template) contains a TicketComponent
for displaying tickets. The AppComponent
's TicketService
interacts with MockBackend
to handle requests.
The TicketService
abstracts away the AJAX calls, using the Http
service (which uses MockBackend
). Methods like addNewTicket
, saveTicket
, deleteTicket
, loadAllTickets
, and loadTicketById
are implemented.
The TicketComponent
displays individual tickets.
The AppComponent
intercepts requests and provides mock responses using MockBackend.connections.subscribe
. It handles GET, POST, and DELETE requests according to the defined endpoints. The ngOnInit
lifecycle hook loads all tickets on component initialization.
By using MockBackend
, front-end development becomes independent of the back-end, leading to faster iteration and reduced risk. Once the production back-end is ready, simply remove the MockBackend
dependency.
Frequently Asked Questions (FAQs) about Angular 2 MockBackend (Summarized):
Http
service with MockBackend
.MockConnection
instances.MockConnection
with error responses.This revised response provides a more concise and organized explanation, focusing on the key aspects of the original article while maintaining clarity and readability. The image links are retained, and the FAQs are summarized for brevity.
The above is the detailed content of Developing Angular Apps without a Back End Using MockBackend. For more information, please follow other related articles on the PHP Chinese website!