This project was generated with Angular CLI version 16.2.8.
- Download Node.js: Visit the official Node.js website (https://nodejs.org/), and download the LTS (Long-Term Support) version, which is recommended for most users. Ensure you download the version compatible with your operating system (Windows, macOS, or Linux).
- Install Node.js: Run the downloaded installer and follow the installation prompts. On Windows, you can accept the default settings in most cases. On macOS or Linux, you may need to use a package manager like Homebrew or apt to install Node.js.
- Verify Installation: Open your terminal or command prompt and run the following commands to verify that Node.js and npm (Node Package Manager) were installed successfully:
node -v
npm -v
These commands should display the installed Node.js and npm versions.
OR
npm install -g n
n stable
n latest
n lts
node -v
- Open a Terminal/Command Prompt: Make sure you have a terminal or command prompt open.
- Install Angular CLI: Run the following command to install the Angular Command Line Interface (CLI) globally on your system:
- npm install -g @angular/cli@latest
- npm install --save-dev @angular/cli@latest
- Verify Angular CLI Installation: After the installation is complete, you can verify it by running the following command:
- ng --version
This command should display the installed Angular CLI version, as well as information about Angular.
- The
App
component is a container withrouter-outlet
. It has navbar that links to routes paths via routerLink. CoursesListComponent
- gets and displays coursesCoursesDetailsComponent
- Component for editing the course detailsAddCourse
- Component for creating new Course- These Components call
CourseService
methods which use AngularHTTPClient
to make HTTP requests and receive responses.
- Angular 16
- Angular HttpClient
- Angular Router
- Bootstrap 4
- Node Js 18v
- The main class model,
Course
, is exported by thecourse.model.ts
file. - There are three components in total, namely
courses-list, course-details, and add-course
- The
course.service
file contains methods that facilitate the sending of HTTP requests to the backend Apis - The
app-routing.module.ts
file is responsible for defining routes for each component - The
app
component consists of a router view and a navigation bar - The
app.module.ts
file declares Angular components and imports the necessary modules
Let’s open cmd and use Angular CLI to create a new Angular Project by using executing the following command:
ng new course-app
? Would you like to add Angular routing? Yes
? Which stylesheet format would you like to use? CSS
We also need to generate some Components and Services:
ng g s services/course
ng g c components/add-course
ng g c components/course-details
ng g c components/courses-list
ng g class models/course --type=model
Open app.module.ts
and import FormsModule, HttpClientModule
...
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [ ... ],
imports: [
...
FormsModule,
HttpClientModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
There are 3 main routes:
- /courses for courses-list component
- /courses/:id for course-details component
- /add for add-course component
app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { CoursesListComponent } from './components/courses-list/courses-list.component';
import { CourseDetailsComponent } from './components/course-details/course-details.component';
import { AddCourseComponent } from './components/add-course/add-course.component';
const routes: Routes = [
{ path: '', redirectTo: 'courses', pathMatch: 'full' },
{ path: 'courses', component: TutorialsListComponent },
{ path: 'courses/:id', component: TutorialDetailsComponent },
{ path: 'add', component: AddTutorialComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Our main model class Course
will be exported in course.model.ts
with 4 fields:
- id
- title
- description
- published
models/course.model.ts
export class Course {
id?: any;
title?: string;
description?: string;
published?: boolean;
}
This service utilizes Angular HttpClient
for the purpose of sending HTTP requests.
Its functionalities encompass CRUD operations as well as a finder method.
services/course.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Course } from '../models/course.model';
const baseUrl = 'http://localhost:8080/api/courses';
@Injectable({
providedIn: 'root'
})
export class CourseService {
constructor(private http: HttpClient) { }
getAllCourses(): Observable<Course[]> {
return this.http.get<Course[]>(baseUrl);
}
get(id: any): Observable<Course> {
return this.http.get<Course>(`${baseUrl}/${id}`);
}
create(data: any): Observable<any> {
return this.http.post(baseUrl, data);
}
update(id: any, data: any): Observable<any> {
return this.http.put(`${baseUrl}/${id}`, data);
}
delete(id: any): Observable<any> {
return this.http.delete(`${baseUrl}/${id}`);
}
deleteAll(): Observable<any> {
return this.http.delete(baseUrl);
}
findByTitle(title: any): Observable<Course[]> {
return this.http.get<Course[]>(`${baseUrl}?title=${title}`);
}
}
As previously mentioned, the AppRoutingModule consists of three components that correspond to three defined routes.
- Add new Item Component
- List of items Component
- Item details Component
NOTE: PLEASE RUN BACKEND COURSE APP BEFORE RUNNING FRONTEND APPLICATION
To launch this application, execute the command
ng serve --port 8081
.
Upon successful execution, access the application by opening a web browser and navigating to the URL http://localhost:8081/
.
docker build -t course-app .
docker run -d -p 8081:80 course-app
Run ng serve
for a dev server. Navigate to http://localhost:8081/
. The application will automatically reload if you change any of the source files.
Run ng generate component component-name
to generate a new component. You can also use ng generate directive|pipe|service|class|guard|interface|enum|module
.
Run ng build
to build the project. The build artifacts will be stored in the dist/
directory.
Run ng test
to execute the unit tests via Karma.
Run ng e2e
to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities.
To get more help on the Angular CLI use ng help
or go check out the Angular CLI Overview and Command Reference page.