MERN STACK LAB MODULE final cse
MERN STACK LAB MODULE final cse
Angular requires Node.js and npm (Node Package Manager), which are included with Node.js installation.
node -v
npm -v
```
The Angular CLI (Command Line Interface) is used to create, build, and manage Angular projects.
ng new project-name
```
You will be prompted to configure the project. Choose the appropriate options like adding Angular routing and
selecting the style format (CSS, SCSS, etc.).
### 4. **Navigate to the Project Folder**
cd project-name
```
ng serve
```
- Open a browser and navigate to `http://localhost:4200/` to see your Angular app running.
1B) Create a new component called hello and render hello on the page
1c)Add a event to hello component template and when it is clicked it should change the name
App.component.ts
Code:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
standalone: true, // Marking the component as standalone
template: `
<div class="hello-card" (click)="toggleUser()">
<h2>Hello, {{ currentUser }}!</h2>
</div>
`,
styles: [`
.hello-card {
background-color: #f9f9f9;
border: 1px solid #ddd;
border-radius: 8px;
padding: 20px;
text-align: center;
max-width: 300px;
margin: 50px auto;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
cursor: pointer;
}
`]
})
export class AppComponent {
currentUser: string = 'User 1';
toggleUser() {
this.currentUser = this.currentUser === 'User 1' ? 'User 2' : 'User 1';
}
}
2A)create a login form with user name and password fields if the user enter the correct
details it should render a welcome otherwise invalid login using “ngif” in angular
code:
App.component.ts
@Component({
selector: 'app-root',
standalone: true, // Use standalone component
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
imports: [FormsModule, CommonModule], // Importing necessary modules
})
export class AppComponent {
username: string = '';
password: string = '';
isAuthenticated: boolean = false;
invalidCredentials: boolean = false;
correctUsername = 'user123';
correctPassword = 'pass123';
login() {
if (this.username === this.correctUsername && this.password ===
this.correctPassword) {
this.isAuthenticated = true;
this.invalidCredentials = false;
} else {
this.invalidCredentials = true;
this.isAuthenticated = false;
}
}
}
2B)use “ngfor” Create a courses array and rendering it in the template using ngFor directive in
a list format.
Code: (app.component.ts)
@Component({
selector: 'app-root',
standalone: true,
template: `
<h1>Fruit List</h1>
<ul>
{{ fruit }}
</li>
</ul>
`,
imports: [CommonModule]
})
selectFruit(fruit: string) {
}}
2C)display the correct option based on value passed to the “ngswitch” directive
Code(app.component.ts):
@Component({
selector: 'app-root',
standalone: true,
template: `
<div class="container">
<h1>Fruit List</h1>
<ul class="fruit-list">
{{ fruit }}
</li>
</ul>
<p *ngSwitchCase="'Banana'">Bananas are elongated, edible fruits produced by several kinds of large
herbaceous flowering plants.</p>
<p *ngSwitchCase="'Orange'">Oranges are juicy citrus fruits that are sweet and tangy.</p>
<p *ngSwitchCase="'Mango'">Mangoes are a stone fruit known for their sweetness and vibrant color.</p>
<p *ngSwitchCase="'Grapes'">Grapes are small, sweet fruits that grow in clusters on vines.</p>
</div>
</div>
`,
styles: [`
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #f9f9f9;
border-radius: 8px;
h1 {
text-align: center;
color: #333;
.fruit-list {
list-style-type: none;
padding: 0;
.fruit-item {
padding: 10px;
margin: 5px 0;
background-color: #e0f7fa;
border-radius: 4px;
cursor: pointer;
.fruit-item:hover {
background-color: #b2ebf2;
.fruit-details {
margin-top: 20px;
padding: 10px;
background-color: #fff3e0;
border-radius: 4px;
h2 {
color: #555;
`],
imports: [CommonModule]
})
selectFruit(fruit: string) {
this.selectedFruit = fruit;
}}
2D)create custom structural directive called repeat which should repeat the element given a
number of times.
@Directive({
selector: '[repeat]',
standalone: true // Mark the directive as standalone
})
export class RepeatDirective {
@Input() set repeat(times: number) {
this.viewContainer.clear(); // Clear any existing views
for (let i = 0; i < times; i++) {
this.viewContainer.createEmbeddedView(this.templateRef); // Create the
element specified times
}
}
@Component({
selector: 'app-root',
template: `
<div>
<label for="repeatTimes">Enter number of repetitions: </label>
<input id="repeatTimes" type="number" [(ngModel)]="repeatCount" />
</div>
<div *repeat="repeatCount">
<p>This element is repeated {{ repeatCount }} times!</p>
</div>
`,
standalone: true,
imports: [RepeatDirective, FormsModule],
})
export class AppComponent {
repeatCount = 1; // Default value for repetitions
}
1D)progressively building poolcarz applications
car.service.ts
code:
id: number;
model: string;
year: number;
@Injectable({
providedIn: 'root'
})
];
getCars(): Car[] {
return this.cars;
this.cars.push(car);
@Component({
selector: 'app-car',
standalone: true,
template: `
<div class="container">
<h2>PoolCarz Management</h2>
<div class="input-group">
<input
[(ngModel)]="newCar.model"
[ngModelOptions]="{standalone: true}"
placeholder="Car Model"
required
/>
<input
[(ngModel)]="newCar.year"
type="number"
[ngModelOptions]="{standalone: true}"
placeholder="Year"
required
/>
</div>
</form>
<ul class="car-list">
</ul>
</div>
`,
styles: [`
.container {
max-width: 600px;
margin: auto;
padding: 20px;
border-radius: 8px;
background-color: #f9f9f9;
h2 {
text-align: center;
margin-bottom: 20px;
color: #333;
}
.input-group {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
input {
flex: 1;
padding: 10px;
margin-right: 10px;
border-radius: 4px;
input:focus {
outline: none;
button {
border: none;
border-radius: 4px;
cursor: pointer;
button:hover {
.car-list {
list-style-type: none;
padding: 0;
margin-top: 20px;
.car-item {
padding: 10px;
.car-item:hover {
}
@media (max-width: 600px) {
.input-group {
flex-direction: column;
input {
margin-right: 0;
margin-bottom: 10px;
`],
encapsulation: ViewEncapsulation.None,
})
ngOnInit(): void {
this.cars = this.carService.getCars();
addCar(): void {
this.newCar = { id: 0, model: '', year: new Date().getFullYear() }; // Reset the form
}}
App.routing.ts
Code:
];
3B)apply multiple css classes to the text using ng class directive
Code:
modify responsivetext.component.ts
code:
@Component({
selector: 'app-responsive-text',
standalone: true,
template: `
</div>
<div class="button-container">
</div>
`,
styles: [`
.text-container {
font-size: 16px;
padding: 10px;
margin: 20px;
.large {
font-size: 24px;
color: blue;
.highlight {
background-color: yellow;
.bold {
font-weight: bold;
.italic {
font-style: italic;
.underline {
text-decoration: underline;
.red {
color: red;
.green {
color: green;
.blue {
color: blue;
.button-container {
margin: 20px;
align-items: center;
display:flex;
button {
margin-right: 5px;
padding: 10px;
cursor: pointer;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
button:hover {
background-color: #0056b3;
`],
imports: [CommonModule]
})
export class ResponsiveTextComponent {
isLarge = false;
isHighlighted = false;
isBold = false;
isItalic = false;
isUnderlined = false;
isRed = false;
isGreen = false;
isBlue = false;
get textClasses() {
return {
large: this.isLarge,
highlight: this.isHighlighted,
bold: this.isBold,
italic: this.isItalic,
underline: this.isUnderlined,
red: this.isRed,
green: this.isGreen,
blue: this.isBlue
};
private toggleableStyles = [
'isLarge',
'isHighlighted',
'isBold',
'isItalic',
'isUnderlined',
'isRed',
'isGreen',
'isBlue'
] as const;
this[style] = !this[style];
app.routing.ts
code:
];
3a)apply multiple css properties to a paragraph in a component using “ngstyle”
modify para.component.ts
code:
@Component({
selector: 'app-para',
standalone: true,
imports: [CommonModule],
templateUrl: './para.component.html',
styleUrls: ['./para.component.css']
})
paragraphStyles = {
fontSize: '18px',
textAlign: 'justify',
backgroundColor: '#f9f9f9',
margin: '10px',
};
paragraphContent = "This is a responsive paragraph styled using Angular's ngStyle. You can easily change the
styles to fit your design.";
App.routing.ts
];
3c)Custom attribute directive
Create a .ts file in app directory
Show-message.directive.ts
Code:
@Directive({
selector: '[appShowMessage]',
standalone: true
})
@HostListener('click') onClick() {
if (!this.messageElement) {
this.el.nativeElement.innerHTML = '';
this.messageElement = document.createElement('p');
this.messageElement.textContent = this.message;
this.messageElement.style.margin = '10px 0';
this.el.nativeElement.appendChild(this.messageElement);
this.buttonElement = document.createElement('button');
this.buttonElement.style.marginTop = '10px';
this.buttonElement.style.cursor = 'pointer';
this.el.nativeElement.appendChild(this.buttonElement);
});
private toggleColor() {
this.isRed = !this.isRed;
@Component({
selector: 'app-message-display',
standalone: true,
template: `
</button>
</div>
`,
styles: [],
})
];