Wednesday, October 26, 2022

How Components Communicate With Each Other In Angular


How Components Communicate With Each Other In Angular

 Components are the building blocks of Angular. So we need to understand how components communicate with each other.

There are three ways:

  1. parent to child - sharing data via input 
  2. child to parent - sharing data via viewChild with AfterViewInit
  3. child to parent - sharing data via output and EventEmitter
How Components Communicate With Each Other In Angular

Parent to child - sharing data via input
To share data from parent component to child component we use@input decorator.

Let’s configure the parent component and child component,
import { Component } from '@angular/core';

@Component({
  selector: 'app-parent',
  template: ` <app-child [childMessage]="message"></app-child> `,
  styleUrls: ['./parent.component.css']
})

export class ParentComponent {
  message = “I am a parent"
  constructor() { }
}

If you see the above code, <app-child> is used as a directive in the parent component. we are using property binding to bind the properties between child and parent, Binding child’s childMessage property with message property from the parent component. 

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

@Component({
  selector: 'app-child',
  template: ` {{ message }}  `,
  styleUrls: ['./child.component.css']
})
export class ChildComponent {
  @Input() childMessage: string;
  constructor() { }
}

To use@input decorative, we have to first import it from @angular/core . Next will decorate childMessage with @input decorative with type string. Lastly, display the message from the parent into the child template using string interpolation.


Child to parent - sharing data via ViewChild and AfterViewInit
It allows the child component to be injected into parent competent so that the parent component gets access to its child’s attributes and functions. We must note that parents will get access only after the view is initialized.

Let’s configure the parent component and child component

import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { ChildComponent } from "../child/child.component";

@Component({
  selector: 'app-parent',
  template: `
    Message: {{ message }}
    <app-child></app-child>
  `,
  styleUrls: ['./parent.component.css']
})

export class ParentComponent implements AfterViewInit {

  @ViewChild(ChildComponent) child;
  constructor() { }

  Message:string;

  ngAfterViewInit() {
    this.message = this.child.message
  }
}
JavaScript
import { Component} from '@angular/core';

@Component({
  selector: 'app-child',
  template: `  `,
  styleUrls: ['./child.component.css']
})

export class ChildComponent {
  message = 'Hello Angular!';
  constructor() { }
}

We have to import ViewChild,AfterViewInit from angular/core, and the child component and use AfterViewInit life cycle hook so that we get access to the attributes in the child component. In the child component, we are passing the value for the message.

Child to parent - sharing data via output and EventEmitter
You use this approach when you want to share the data on button clicks or other events. The child component uses @output to send the data to the parent by creating an event with the help of an EventEmitter which is imported from @angular/core.

Let’s configure the parent component and child component.
import { Component, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `
      <button (click)="message()">Send Message</button>
 `,
  styleUrls: ['./child.component.css']
})
export class ChildComponent {
  message: string = "Hey I am child !"
  @Output() messageEvent = new EventEmitter<string>();
 constructor() { }
  sendMessage() {
    this.messageEvent.emit(this.message)
  }
}

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

@Component({
  selector: 'app-parent',
  template: `
    Message: {{message}}
    <app-child (messageEvent)="receiveMessage($event)"></app-child>
  `,
  styleUrls: ['./parent.component.css']
})
export class ParentComponent {
    constructor() { }
    Message:string;
    receiveMessage($event) {
        this.message = $event
    }
}
JavaScript
First, we have to import output and EventEmitter from the @anguar/core , then we are creating an event messageEvent using EventEmitter . when the button is clicked it calls the function sendMessage and calls the emit with the message. we are using event binding property with the child selector . The parent can now subscribe to this messageEvent that’s outputted by the child component, then run the receive message function whenever this event occurs.

Wednesday, October 19, 2022

Interview questions 2022 angular

 

If Record Exists, Update Else Insert

MERGE
INTO    table2 t2
USING   table1 t1
ON      t2.email = t1.email
WHEN MATCHED THEN
UPDATE
SET     t2.col1 = t1.col1,
        t2.col2 = t1.col2
WHEN NOT MATCHED THEN
INSERT  (col1, col2)
VALUES  (t1.col1, t1.col2)

JSON_QUERY (Transact-SQL)

https://www.google.com/search?q=communicatingdata+in+angular&rlz=1C1RXQR_enIN1016IN1016&oq=communicatingdata+in++angular&aqs=chrome..69i57j0i546l2j0i30i546.21656j0j7&sourceid=chrome&ie=UTF-8

How Components Communicate With Each Other In Angular


https://www.google.com/search?rlz=1C1RXQR_enIN1016IN1016&sxsrf=ALiCzsaMQQtDQWVTu16g8FhyLGFO2KBDJA:1666160359941&q=communicating+data+in+angular&spell=1&sa=X&ved=2ahUKEwjclJKD0-v6AhXwSmwGHatdDfIQkeECKAB6BAgFEAE


https://www.thirdrocktechkno.com/blog/how-angular-components-communicate/

https://www.dotnettricks.com/learn/angular/top-20-angular-interview-questions-and-answers

https://www.thirdrocktechkno.com/blog/how-angular-components-communicate/

https://www.sqlshack.com/understanding-the-sql-merge-statement/

https://www.google.com/search?q=check+if+record+exists+in+temp+table+update+else+insert+sql+server&rlz=1C1RXQR_enIN1016IN1016&oq=check+if+record+exists+in+temp+table+update+else+insert+sql+server&aqs=chrome.0.69i59.10573j0j7&sourceid=chrome&ie=UTF-8

https://www.google.com/search?q=how+to+read+json+output+from+sql+server&rlz=1C1RXQR_enIN1016IN1016&sxsrf=ALiCzsZk2vN_38ZOMDL0an4CNUzES5CNiA%3A1666162997803&ei=NaFPY4HSMO_gseMPnM68WA&ved=0ahUKEwjBwPzs3Ov6AhVvcGwGHRwnDwsQ4dUDCA8&uact=5&oq=how+to+read+json+output+from+sql+server&gs_lcp=Cgdnd3Mtd2l6EAMyBQgAEKIEMgcIABAeEKIEMgUIABCiBDIFCAAQogQyBwgAEB4QogQ6CggAEEcQ1gQQsAM6CgghEMMEEAoQoAFKBAhNGAFKBAhBGABKBAhGGABQ5gpYsB1gmSloAnABeACAAdUCiAG0CJIBBzAuMy4xLjGYAQCgAQHIAQjAAQE&sclient=gws-wiz

https://www.google.com/search?q=outer+apply+in+sql&rlz=1C1RXQR_enIN1016IN1016&oq=outer+ap&aqs=chrome.0.0i20i263i512j0i512l9.4755j0j7&sourceid=chrome&ie=UTF-8

https://www.google.com/search?q=coalesce+in+sql&rlz=1C1RXQR_enIN1016IN1016&oq=collase&aqs=chrome.2.69i57j0i10i131i433i512l2j0i10i512l6j0i10i433i512.5622j0j7&sourceid=chrome&ie=UTF-8

https://www.google.com/search?q=check+if+record+exists+in+temp+table+update+else+insert+sql+server&rlz=1C1RXQR_enIN1016IN1016&sxsrf=ALiCzsb5J4ANp-dMWmBK1g31YUioHii-Gw%3A1666162689925&ei=AaBPY-2AOMa7seMPz6S10Ak&ved=0ahUKEwithZXa2-v6AhXGXWwGHU9SDZoQ4dUDCA8&uact=5&oq=check+if+record+exists+in+temp+table+update+else+insert+sql+server&gs_lcp=Cgdnd3Mtd2l6EAM6CggAEEcQ1gQQsAM6BwgjELACECc6BQgAEIYDOgQIIxAnOgUIABCiBDoHCAAQHhCiBDoECCEQCkoECE0YAUoECEEYAEoECEYYAFCXJliAemDegQFoAXABeACAAe4BiAHwGZIBBjAuMjAuMZgBAKABAcgBCMABAQ&sclient=gws-wiz


https://www.google.com/search?q=register+filter+in+web+api&rlz=1C1RXQR_enIN1016IN1016&oq=register+filter&aqs=chrome.1.69i57j0i20i263i512j0i512l8.12903j0j7&sourceid=chrome&ie=UTF-8

https://www.google.com/search?q=get+type+of+input+c%23&rlz=1C1RXQR_enIN1016IN1016&oq=get+type+of+input+c%23&aqs=chrome..69i57j33i22i29i30l4.11392j0j7&sourceid=chrome&ie=UTF-8


https://www.google.com/search?q=genec+method+c%23&rlz=1C1RXQR_enIN1016IN1016&oq=genec+method+c%23&aqs=chrome..69i57j0i13i512l7j0i13i30l2.9752j1j7&sourceid=chrome&ie=UTF-8

generic methods with example


https://www.google.com/search?q=check+if+string+is+valid+json+webabi+c%23&rlz=1C1RXQR_enIN1016IN1016&sxsrf=ALiCzsbGPwwZAkAwxiue9qOP5J0ZFZU9fQ%3A1666162262959&ei=Vp5PY-CMOsaaseMPoai-4AU&ved=0ahUKEwjgj8mO2uv6AhVGTWwGHSGUD1wQ4dUDCA8&uact=5&oq=check+if+string+is+valid+json+webabi+c%23&gs_lcp=Cgdnd3Mtd2l6EAMyBQgAEKIEMgUIABCiBDIHCAAQHhCiBDoKCAAQRxDWBBCwA0oECE0YAUoECEEYAEoECEYYAFC0BFj2RGDgSmgCcAF4AIABnwKIAY8MkgEFMC41LjOYAQCgAQHIAQjAAQE&sclient=gws-wiz


https://www.google.com/search?q=provide+action+verb+c%23&rlz=1C1RXQR_enIN1016IN1016&sxsrf=ALiCzsapZkev1U4CxHSxsDfZBcV0iFqWRw%3A1666162169916&ei=-Z1PY6bCN6mfseMP982DqA0&ved=0ahUKEwjmopri2ev6AhWpT2wGHffmANUQ4dUDCA8&uact=5&oq=provide+action+verb+c%23&gs_lcp=Cgdnd3Mtd2l6EAMyBQghEKABMgUIIRCgAToKCAAQRxDWBBCwA0oECEEYAEoECEYYAFD-AVjnGmD9IWgCcAF4AYAB4ASIAdQIkgEHMC4zLjUtMZgBAKABAcgBCMABAQ&sclient=gws-wiz


https://www.google.com/search?q=http+request+angular+header&rlz=1C1RXQR_enIN1016IN1016&oq=http+request+angular+header&aqs=chrome..69i57j0i22i30l9.10903j1j7&sourceid=chrome&ie=UTF-8


https://stackoverflow.com/questions/41133705/how-to-correctly-set-http-request-header-in-angular-2


https://www.google.com/search?q=parent+to+child+data+transfer+in+angular&rlz=1C1RXQR_enIN1016IN1016&oq=parent+to&aqs=chrome.1.69i57j0i20i263i512j0i512l8.6322j0j7&sourceid=chrome&ie=UTF-8

https://www.google.com/search?rlz=1C1RXQR_enIN1016IN1016&sxsrf=ALiCzsZ1rDo_cMXDQzP74kAbQmwgPummYw:1666161767774&q=web+organs+angular&spell=1&sa=X&ved=2ahUKEwimuLmi2Ov6AhVoTGwGHR-VAucQBSgAegQIBxAB&biw=1280&bih=569&dpr=1.5

https://www.google.com/search?rlz=1C1RXQR_enIN1016IN1016&sxsrf=ALiCzsZ2c_b9mUp2AjjVpeZ_BAGfEWoaNQ:1666161594154&q=to+navigate+from+admin+to+another+page+typescript&spell=1&sa=X&ved=2ahUKEwiB0dTP1-v6AhXNcGwGHfJfA3kQBSgAegQIBhAB&biw=1280&bih=569&dpr=1.5


https://www.google.com/search?q=initialize+reactive+form+angular&rlz=1C1RXQR_enIN1016IN1016&oq=initialize+rea&aqs=chrome.2.0i512l4j69i57j0i512l5.10054j0j7&sourceid=chrome&ie=UTF-8


https://www.google.com/search?q=subject+vs+behaviorsubject&rlz=1C1RXQR_enIN1016IN1016&oq=subject+&aqs=chrome.2.69i57j0i131i433i512j0i433j0i433i512j0i512j0i20i263i512j0i433i512j69i65.7153j0j7&sourceid=chrome&ie=UTF-8


https://www.google.com/search?q=how+to+pass+data+between+two+independent+components+in+angular&rlz=1C1RXQR_enIN1016IN1016&oq=data+between+two+independent+c&aqs=chrome.1.69i57j0i22i30l2.17941j0j7&sourceid=chrome&ie=UTF-8


https://www.google.com/search?q=%40output+in+angular&rlz=1C1RXQR_enIN1016IN1016&oq=%40output&aqs=chrome.1.69i57j0i512l6j69i65.9822j0j7&sourceid=chrome&ie=UTF-8



Tuesday, October 18, 2022

environment variable on windows 10

 Modifying PATH on Windows 10:

  1. In the Start Menu or taskbar search, search for "environment variable".
  2. Select "Edit the system environment variables".
  3. Click the "Environment Variables" button at the bottom.
  4. Double-click the "Path" entry under "System variables".
  5. With the "New" button in the PATH editor, add C:\Program Files\Git\bin\ and C:\Program Files\Git\cmd\ to the end of the list.
  6. Close and re-open your console.

Sunday, October 16, 2022

Angular Interview questions

Fundamentals of Angular 

Component

Module

Data bindings

Services

Dependency Injection

Directives

Templates

Application Bootstrapping

Navigation or Routing

Native mobile development

Angular Elements

Angular Universal


What are the features of Angular CLI?

Angular CLI is a powerful command-line tool by which we can create new files, update the file, build and deploy the angular application and other features are available so that we can get started with Angular development within a few minutes.

  • Packaging application and release for the deployment

  • Testing an Angular app

  • Bootstrapping Angular app

  • Various code generation options for Component, Module, Class, Pipes, Services, Enums, and many other types are also supported.

  • Running the unit test cases

  • Build and deployment our Angular application


What are the advantages of the Angular Console?

There are a few advantages and features provided by the Angular Console.

  • Build CLI Commands visually

  • Internal terminal for the output

  • Import the existing Angular project

  • Trivial code generation

  • Run the custom NPM scripts

  • Install various extensions


What is a Component in Angular?
Components are the basic building block of an Angular application. An Angular component mainly contains a template, class, and metadata. It is used to represent the data visually. A component can be thought of as a web page. An Angular component is exported as a custom HTML tag like as: <my-app></my-app>.
For creating a component, we can use decorator @component 

ng generate component democomponent

What is the Template Expression?
Template expressions are the same expression that we use with JavaScript. But the difference is that we can use it within our Html template, so it looks like we are using JavaScript along with Html 


What is Data Binding in Angular?
Data binding is one of the important core concepts which is used to do the communicate between the component and DOM. In other words, we can say that Data binding is a way to add/push elements into HTML Dom or pop the different elements based on the instructions given by the Component. There are mainly three types of data binding support in order to achieve the data bindings in Angular.

One-way Binding (Interpolation, Attribute, Property, Class, Style)

Event Binding

Two-way Binding

Using these different binding mechanisms, we can easily communicate the component with the DOM element and perform various operations.

What is Interpolation?
Interpolation in Angular is used to show the property value from the component to the template or used to execute the expressions. Generally, interpolation can be used to execute the JavaScript expressions and append the result to the Html DOM. Another use of Interpolation is to print the data or we can say one-way data, which is coming from the component i.e. from a variable or the item of an array. And interpolation can be used to achieve Property binding as well by using curly braces {{ }}, let’s see a simple example.


App.component.ts
 myMessage: string = "Hello World";

App.component.html
 <p>{{ myMessage }}</p>
 <p>Total : {{ 25 + 50 + 75 + 100 }}</p>


Directives in angular?
directives are angular sytaxces which go and change the behaviour of html dom