Angular Routing & Forms based Demo application

This commit is contained in:
rupeshbangar
2026-03-12 12:39:47 +05:30
parent 15fc491d9a
commit 6325edc3a5
32 changed files with 9292 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
export const appConfig = {
providers: [provideRouter(routes)]
};

View File

@@ -0,0 +1,10 @@
<h1>Student Portal</h1>
<nav>
<a routerLink="/">Home</a> |
<a routerLink="/register">Register</a>
</nav>
<hr>
<router-outlet></router-outlet>

View File

@@ -0,0 +1,11 @@
import { Routes } from '@angular/router';
import { Home } from './home/home';
import { RegisterComponent } from './register/register';
export const routes: Routes = [
{ path: '', component: Home },
{ path: 'register', component: RegisterComponent }
];

View File

@@ -0,0 +1,23 @@
import { TestBed } from '@angular/core/testing';
import { App } from './app';
describe('App', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [App],
}).compileComponents();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(App);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
it('should render title', async () => {
const fixture = TestBed.createComponent(App);
await fixture.whenStable();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.querySelector('h1')?.textContent).toContain('Hello, student-routing-form');
});
});

View File

@@ -0,0 +1,12 @@
import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, RouterModule],
templateUrl: './app.html',
styleUrls: ['./app.css']
})
export class AppComponent {}

View File

@@ -0,0 +1,9 @@
h2{
text-align:center;
color:#2c3e50;
}
p{
text-align:center;
font-size:18px;
}

View File

@@ -0,0 +1,6 @@
<h2>Welcome to Student Portal</h2>
<p>
This is the home page of the application.
Click Register to fill the student form.
</p>

View File

@@ -0,0 +1,22 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Home } from './home';
describe('Home', () => {
let component: Home;
let fixture: ComponentFixture<Home>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [Home],
}).compileComponents();
fixture = TestBed.createComponent(Home);
component = fixture.componentInstance;
await fixture.whenStable();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@@ -0,0 +1,11 @@
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-home',
standalone: true,
imports: [CommonModule],
templateUrl: './home.html',
styleUrls: ['./home.css'],
})
export class Home {}

View File

@@ -0,0 +1,46 @@
h2{
text-align:center;
color :#2c3e50;
}
form{
width:350px;
margin:auto;
padding:20px;
background:white;
border-radius:8px;
box-shadow:0 0 10px rgba(0,0,0,0.1);
}
label{
font-weight:bold;
}
input{
width:100%;
padding:8px;
margin-top:5px;
border:1px solid #ccc;
border-radius:4px;
}
button{
margin-top:15px;
width:100%;
padding:10px;
background:#3498db;
color:white;
border:none;
border-radius:4px;
cursor:pointer;
}
button:hover{
background:#2980b9;
}
.error{
color:red;
font-size:13px;
margin-top:5px;
}

View File

@@ -0,0 +1,75 @@
<h2>Student Registration Form</h2>
<form #studentForm="ngForm" (ngSubmit)="onSubmit(studentForm)">
<!-- Name -->
<label>Name:</label>
<input type="text" name="name" ngModel required #name="ngModel">
<div class="error" *ngIf="name.invalid && name.touched">
Name is required
</div>
<br><br>
<!-- Email -->
<label>Email:</label>
<input type="email"
name="email"
ngModel
required
email
#email="ngModel">
<div class="error" *ngIf="email.invalid && email.touched">
<div *ngIf="email.errors?.['required']">
Email is required
</div>
<div *ngIf="email.errors?.['email']">
Enter valid email (must include @)
</div>
</div>
<br><br>
<!-- Password -->
<label>Password:</label>
<input type="password"
name="password"
ngModel
required
minlength="8"
#password="ngModel">
<div class="error" *ngIf="password.invalid && password.touched">
<div *ngIf="password.errors?.['required']">
Password is required
</div>
<div *ngIf="password.errors?.['minlength']">
Password must be at least 8 characters
</div>
</div>
<br><br>
<!-- Course -->
<label>Course:</label>
<input type="text" name="course" ngModel required #course="ngModel">
<div class="error" *ngIf="course.invalid && course.touched">
Course is required
</div>
<br><br>
<button type="submit" [disabled]="studentForm.invalid">
Submit
</button>
</form>

View File

@@ -0,0 +1,22 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { Register } from './register';
describe('Register', () => {
let component: Register;
let fixture: ComponentFixture<Register>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [Register],
}).compileComponents();
fixture = TestBed.createComponent(Register);
component = fixture.componentInstance;
await fixture.whenStable();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@@ -0,0 +1,25 @@
import { Component } from '@angular/core';
import { FormsModule, NgForm } from '@angular/forms';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-register',
standalone: true,
imports: [FormsModule, CommonModule],
templateUrl: './register.html',
styleUrl: './register.css'
})
export class RegisterComponent {
onSubmit(form: NgForm) {
console.log(form.value);
alert("Form Submitted Successfully!");
// reset form
form.reset();
}
}

View File

@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>StudentRoutingForm</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
</body>
</html>

View File

@@ -0,0 +1,6 @@
import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app';
bootstrapApplication(AppComponent, appConfig)
.catch((err) => console.error(err));

View File

@@ -0,0 +1,33 @@
body{
font-family: Arial, Helvetica, sans-serif;
margin:0;
padding:0;
background:#f4f6f8;
}
h1{
text-align:center;
background:#2c3e50;
color:white;
padding:15px;
}
nav{
text-align:center;
margin:20px;
}
nav a{
text-decoration:none;
color:#2c3e50;
font-weight:bold;
margin:10px;
}
nav a:hover{
color:#3498db;
}
hr{
margin:20px;
}