This project is an Angular-based application designed to handle category forms and manage categories. It features form validation, integration with a backend service, and a clean user interface.
- Dynamic form creation using Angular Reactive Forms
- Form validation for user inputs
- Category management (add, view, edit, delete)
- Integration with a backend API for data retrieval and updates
- Modular structure for scalability and maintainability
Before you begin, ensure you have met the following requirements:
- Node.js and npm installed on your computer
- Angular CLI installed globally (
npm install -g @angular/cli
) - A running backend API for category data (optional, depending on the project setup)
-
Clone the repository:
git clone https://github.com/Seymagocmez/AngularFormApp.git
-
Navigate to the project directory:
cd AngularFormApp
-
Install dependencies:
npm install
-
Start the development server:
ng serve
-
Open your browser and navigate to:
http://localhost:4200
-
Use the application to manage categories by filling out forms and interacting with the UI.
Angular's Reactive Forms module provides a flexible and scalable way to build forms.
-
Import ReactiveFormsModule: Ensure
ReactiveFormsModule
is imported in yourAppModule
:import { ReactiveFormsModule } from '@angular/forms'; @NgModule({ declarations: [ AppComponent, ], imports: [ BrowserModule, ReactiveFormsModule, ], bootstrap: [AppComponent] }) export class AppModule { }
-
Define the Form Group: In your component, create a
FormGroup
:import { Component, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; @Component({ selector: 'app-service-page', templateUrl: './service-page.component.html', }) export class ServicePageComponent implements OnInit { categoryForm: FormGroup; constructor(private fb: FormBuilder) {} ngOnInit(): void { this.categoryForm = this.fb.group({ categoryId: [null], categoryName: ['', Validators.required], description: ['', Validators.required] }); } onSubmit(): void { console.log(this.categoryForm.value); } }
-
Bind the Form to the Template: Use Angular's template syntax to connect your form:
<form [formGroup]="categoryForm" (ngSubmit)="onSubmit()"> <label for="categoryId">Category ID:</label> <input id="categoryId" formControlName="categoryId"> <label for="categoryName">Category Name:</label> <input id="categoryName" formControlName="categoryName"> <label for="description">Description:</label> <textarea id="description" formControlName="description"></textarea> <button type="submit">Submit</button> </form>
Angular services facilitate communication with backend APIs. Here's how to set one up:
-
Generate a Service: Run the Angular CLI command:
ng generate service services/category
-
Define the Service Logic: Implement methods to interact with the backend:
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; import { Category } from '../models/category.model'; @Injectable({ providedIn: 'root' }) export class CategoryService { private apiUrl = 'https://example.com/api/categories'; constructor(private http: HttpClient) {} getCategories(): Observable<Category[]> { return this.http.get<Category[]>(this.apiUrl); } addCategory(category: Category): Observable<Category> { return this.http.post<Category>(this.apiUrl, category); } }
-
Use the Service in a Component: Inject the service and call its methods:
import { Component, OnInit } from '@angular/core'; import { CategoryService } from './services/category.service'; import { Category } from './models/category.model'; @Component({ selector: 'app-service-page', templateUrl: './service-page.component.html', }) export class ServicePageComponent implements OnInit { categories: Category[] = []; constructor(private categoryService: CategoryService) {} ngOnInit(): void { this.categoryService.getCategories().subscribe(data => { this.categories = data; }); } }
Angular is a TypeScript-based front-end framework designed for building dynamic web applications. One of its core principles is modularity:
-
Separation of Concerns: Angular promotes organizing code into distinct modules for components, services, and routing. This ensures that each module has a single responsibility.
-
Reusability: Modules, components, and services can be reused across different parts of the application or even across projects.
-
Lazy Loading: Angular modules can be lazy-loaded to improve application performance by only loading the required modules when needed.
- Add unit tests for components and services
- Implement additional CRUD operations for category management
- Improve error handling and user notifications
This project is open-source and available under the MIT License.
If you have any questions or feedback, feel free to reach out:
- GitHub: Seymagocmez