For this exercise, you may work alone or with another student in this course.
Note: This option is somewhat similar to POTD 5 Angular service and HTTP where you will implement your own service that utilizes Angular HTTP service.
For this option, you will use the standard Angular HTTP service to connect between the frontend and the backend.
Imagine you are writing an Angular app that will accept user's inputs. The Angular program will then send a request (i.e., asynchronous request) to a backend component. A backend component then processes the request and returns a response to the Angular program. The Angular program then uses the response and updates the screen.
You may use an Angular app you implemented for the Angular form and input validation activity as a starting point. Alternatively, you may create an Angular form from scratch or use an Angular form from your project. You will implement a PHP backend component that will accept a request from the Angular and return a response to the Angular.
A sample interface:
A sample interface after receiving a response from the backend:
You may modify your interface the way you like. Get creative. Feel free to add additional elements you feel should be included and have fun!
app.module.ts
):
import { HttpClientModule } from '@angular/common/http';
Describe the dependency the root module has by including
HttpClientModule
in the imports
array
of the @ngModule
declarator
app.component.ts
)
import { HttpClient, HttpErrorResponse, HttpParams } from '@angular/common/http';
HttpClient
— performs HTTP requestsHttpErrorResponse
— represents an error or failureHttpParams
— represents parameters of an HTTP request/responseapp.component.ts
)
constructor(private http: HttpClient) { }
get()
method of the HttpClient
to
send a GET request to the server.
Alternatively, you may use the post()
method of the HttpClient
to
send a POST request to the server.
Be sure to subscribe()
.
// header('Access-Control-Allow-Origin: http://localhost:4200'); header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding'); header('Access-Control-Max-Age: 1000'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT');Refer to dealing with CORS for more information on header setting for cross-site request.
If you host your PHP program on GCP, you need to do the following extra steps.
Create a file named cors-json-file.json containing the following code (to force GCP PHP environment to accept remote request)
[ { "origin": ["*"], "method": ["*"] } ]
Use a terminal, run the following command
gsutil cors set cors-json-file.json gs://your-GCP-bucket
To deploy and test your Angular program, please refer to Angular deployment.
To deploy and test your PHP program use one of the following options:
You may modify your interface the way you like. Get creative. Feel free to add additional elements you feel should be included and have fun!
For more practice, try adding more content. Make use modules, components, classes, as well as data binding and directives.
Making your submission available to instructor and course staff is your responsibility; if we cannot access or open your file, you will not get credit. Be sure to test access to your file before the due date.
Released under the CC-BY-NC-SA 4.0 license.
Last updated 2022-06-05 18:30