This project demonstrates template-driven forms in Angular with focused, standalone examples.
[(ngModel)]two-way bindingngFormform state and values(ngSubmit)submit handling- Required and pattern validations
- Disabling submit on invalid form
ngModelGroupnested form objects- Simple custom validation rule
- Typed model usage with template-driven forms
- Form reset patterns
- Full CRUD component using template-driven forms +
json-server
src/app/app.component.html: renders all lesson cardssrc/app/components/components.module.ts: central registry of lesson modulessrc/app/shared/card/*: reusable card UI used by all lessonssrc/styles.scss: shared teaching/demo styles (inputs, states, buttons, layout)
understanding-ng-model: model and control syncunderstanding-ng-form: whole-form state and valueunderstanding-submit: submit event handling withNgFormunderstanding-required-validations:requiredvalidator behaviorunderstanding-pattern-validations: regex/pattern validationunderstanding-disabling: disable submit usingform.invalidunderstanding-ng-model-group: nested form objects with groupsunderstanding-custom-validations: custom business-rule validation (age)understanding-typed-model: typed TS model + template-driven inputsunderstanding-reset: reset empty and reset with defaultsunderstanding-crud-template-driven: full create/read/update/delete flow
- Node.js 18+
- npm 9+
npm installnpm startOpen: http://localhost:4200/
The CRUD lesson uses http://localhost:3000/users.
npm run apiThis reads from db.json.
In understanding-crud-template-driven:
- Read:
ngOnInit()callsloadUsers()from service. - Create: submit form in create mode (
isEditing = false) ->POST /users. - Edit: click
Edit-> patch form model +isEditing = true. - Update: submit in edit mode ->
PUT /users/:id. - Delete: click
Delete->DELETE /users/:id. - Refresh: list reloads after every successful write operation.
Service file:
Terminal 1:
npm run apiTerminal 2:
npm startnpm start- start Angular dev servernpm run api- startjson-serveron port3000npm run build- production buildnpm run watch- development build in watch modenpm test- unit tests
Base URL:
http://localhost:3000/users
User shape:
{
"id": 1,
"name": "Vinay Ranjan",
"email": "vinay@example.com",
"role": "Developer"
}Endpoints:
GET /usersPOST /usersPUT /users/:idDELETE /users/:id
- Root lesson page:
src/app/app.component.html - Component registry:
src/app/components/components.module.ts - CRUD component:
src/app/components/understanding-crud-template-driven/understanding-crud-template-driven.component.tssrc/app/components/understanding-crud-template-driven/understanding-crud-template-driven.component.htmlsrc/app/components/understanding-crud-template-driven/services/crud-template-driven.service.ts
npm run build- API errors in CRUD card:
- Ensure
npm run apiis running on port3000.
- Ensure
- CORS/network issues:
- Confirm app runs on
4200and API on3000(default setup).
- Confirm app runs on
- Empty table:
- Check
db.jsonhas ausersarray.
- Check
- Dependency/script issues:
- Re-run
npm install.
- Re-run
- Add search/filter and client-side pagination to CRUD table.
- Add optimistic UI updates instead of full list reloads.
- Add a dedicated validator directive for reusable custom validations.
