Search This Blog

Q61-Q65

Q61. How you can trigger a function on parent component from a child component?
Q62. what is event emitter in angular?
Q63. What is the usage of @Injectable in service class?
Q64. How you can create a custom Directive? Give real life example of custom directive?
Q65. What are http Interceptors in angular? Are they used in logging?

======================================================================
Q61. How you can trigger a function on parent component from a child component?

Answer:
We can trigger a parent component function from a function defined on  child component using @Output() decorator. For better understanding check #58 which tell how @output is used. 

Follow the below steps to trigger a parent component function from child component function. 
1. Create a function callParent on child component. Defined it on both html and component.ts file. 
2. in callParent function emit the output variable. 
3. in parent component capture the emitted event and call the parent function on that event.

import { Output, EventEmitter } from '@angular/core'; 

...

class ChildComponent {
  @Output() someEvent = new EventEmitter<string>();

  callParent(): void {
    this.someEvent.next('somePhone');
  }
}

on parent component.html
<child-component (someEvent)="deletePhone($event)"></child-component>

======================================================================
Q62. what is event emitter in angular?

Answer:
this is required when we used @output decorator. 
We create a event and emit it from child component. which then captured on parent component. 
for better understanding of @output decorator check #58. 
======================================================================
Q63. What is the usage of @Injectable in service class?

Answer:
Declares that a class has dependencies that should be injected into the constructor when the dependency injector is creating an instance of this class.

a Class become a singleton class when its reference is given in provider section of @NgModule.
======================================================================
Q64. How you can create a custom Directive? Give real life example of custom directive?

Answer:


======================================================================
Q65. What are http Interceptors in angular? Are they used in logging?

Answer:
Interceptors are a unique type of Angular Service that we can implement. Interceptors allow us to intercept incoming or outgoing HTTP requests using the HttpClient . By intercepting the HTTP request, we can modify or change the value of the request

3 practical usage of interceptors
  1. Add a token or some custom HTTP header for all outgoing HTTP requests.
  2. Catch HTTP responses to do some custom formatting (i.e. convert CSV to JSON) before handing the data over to your service/component.
  3. Log all HTTP activity in the console.

No comments:

Post a Comment