AIM: Create an
AppComponent that displays a dropdown with a list of courses as values in it. Create another
component called the CoursesList component
and load it in AppComponent which should display
the course details.
When the user selects a course from the list, the details should be updated.
Program:
Step 1: Create a Course model
src/app/course.model.ts
*******************
export interface Course {
id: number;
name: string;
description: string;
duration: string;
}
********************
Step 2: AppComponent
src/app/app.component.ts
********
import { Component } from '@angular/core';
import { Course } from './course.model';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
courses: Course[] = [
{ id: 1, name: 'Angular Basics', description: 'Learn Angular fundamentals', duration: '4 weeks' },
{ id: 2, name: 'React Fundamentals', description: 'Introduction to React library', duration: '3 weeks' },
{ id: 3, name: 'Node.js Essentials', description: 'Server-side programming with Node.js', duration: '5 weeks' }
];
selectedCourseId: number | null = null;
selectedCourse?: Course;
onCourseSelect(courseId: number) {
this.selectedCourse = this.courses.find(c => c.id === Number(courseId));
}
}
*************
src/app/app.component.html
************
<h1>Course Selection</h1>
<!-- Dropdown with ngModel -->
<select [(ngModel)]="selectedCourseId" (ngModelChange)="onCourseSelect($event)">
<option value="">-- Select a Course --</option>
<option *ngFor="let course of courses" [value]="course.id">
{{ course.name }}
</option>
</select>
<!-- Pass selected course to child -->
<app-courses-list [course]="selectedCourse"></app-courses-list>
**************
generate courses-list component
ng generate component courses-list
src/app/courses-list/courses-list.component.ts code
***************
import { Component, Input } from '@angular/core';
import { Course } from '../course.model';
@Component({
selector: 'app-courses-list',
templateUrl: './courses-list.component.html',
styleUrls: ['./courses-list.component.css']
})
export class CoursesListComponent {
@Input() course?: Course;
}
********************
src/app/courses-list/courses-list.component.html
*******************
<div *ngIf="course; else noCourse">
<h2>{{ course.name }}</h2>
<p><strong>Description:</strong> {{ course.description }}</p>
<p><strong>Duration:</strong> {{ course.duration }}</p>
</div>
<ng-template #noCourse>
<p>Please select a course to see details.</p>
</ng-template>
***********
Add the following code to app.module.ts file in app folder to import forms module
import { FormsModule } from '@angular/forms';
imports: [
BrowserModule,
AppRoutingModule,
FormsModule
],
***************
Run App:
ng serve
6.B Passing data from Child Component to ContainerComponent
AIM: Create an AppComponent that loads another component called the CoursesList component. Create another component called CoursesListComponent which should display the courses list in a table along with a register .button in each row. When a user clicks on the register button it should say you are registered for this course
PROGRAM:
create new App by
ng new c2p
create a component courses-list
PRACTICE
ng generate component courses-list
Comments
Post a Comment