FSD-II Practical 3

 


3.Create a custom life-cycle hook for logging component life-cycle events

 - Define a custom life-cycle hook named `LogLifecycle` that logs component initialization, destruction, and changes in input properties.

- Implement the custom hook in a sample component.

 - Test the behavior by adding the component to a parent component and observing the logs.

 

1.      ng new practical3

2.      cd practical3

3.      ng generate service log-lifecycle

4.       open the file          

log-lifecycle.service.ts

        import { Injectable, OnInit, OnDestroy, OnChanges, SimpleChanges } from '@angular/core';

 

@Injectable({

  providedIn: 'root'

})

export class LogLifecycle implements OnInit, OnDestroy, OnChanges {

 

  constructor() {

    console.log(`${this.constructor.name} - Constructor called`);

  }

 

  ngOnInit(): void {

    console.log(`${this.constructor.name} - Initialized`);

  }

 

  ngOnDestroy(): void {

    console.log(`${this.constructor.name} - Destroyed`);

  }

 

  ngOnChanges(changes: SimpleChanges): void {

    console.log(`${this.constructor.name} - Input properties changed:`, changes);

  }

}

 

 

5.       create sample component

ng generate component sample

   

6.       open sample.component.ts

import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';

import { LogLifecycle } from '../log-lifecycle.service';

 

@Component({

  selector: 'app-sample',

  template: `

    <div>

      <h2>Sample Component</h2>

      <p>Current Value: {{ value }}</p>

      <button (click)="changeValue()">Change Value</button>

    </div>

  `

})

export class SampleComponent extends LogLifecycle implements OnChanges {

  @Input() value: number = 0;

 

  changeValue() {

    this.value = Math.floor(Math.random() * 100); // Randomize value

  }

 

  // Use 'override' because this method overrides the base class method

  override ngOnChanges(changes: SimpleChanges): void {

    super.ngOnChanges(changes); // Call the base class method

  }

}

 

 

7.open app.module.ts as parent component

import { Component } from '@angular/core';

@Component({

 selector: 'app-root',

 template: `

 <div>

 <h1>Parent Component</h1>

 <app-sample [value]="value"></app-sample>

 <button (click)="increaseValue()">Increase Value</button>

 </div>

 `

})

export class AppComponent {

 value: number = 0;

 increaseValue() {

 this.value++;

 }

}

 

8.Run on terminal

      ng serve



Comments

Popular posts from this blog

Slip 11: Write node js application that transfer a file as an attachment on web and enables browser to prompt the user to download file using express js.

Slip 7 Create a node js file named main.js for event-driven application. There should be a main loop that listens for events, and then triggers a callback function when one of those events is detected.

Slip 1(a) Write an Angular 13 application addition of two numbers using ng-init, ng-model & ng-bind. And also demonstrate ng-show, ng-disabled, ng-click directives on button component.