Angular Developer Interview Questions for Freshers
Difference between ng serve and npm start
ng serve
npm start
Difference between 'dependencies' and 'dev-dependencies' properties in package.json
"dependencies": {
"express": "^4.17.1",
"lodash": "^4.17.21"
}
"devDependencies": {
"mocha": "^9.0.3",
"nodemon": "^2.0.13"
}
What are the properties inside @ngModule decorator
Option | Description |
---|---|
providers? | The set of injectable objects that are available in the injector of this module. |
declarations? | The set of components, directives, and pipes (declarables) that belong to this module. |
imports? | The set of NgModules whose exported declarables are available to templates in this module. |
exports? | The set of components, directives, and pipes declared in this NgModule that can be used in the template of any component that is part of an NgModule that imports this NgModule. Exported declarations are the module's public API. |
entryComponents? | The set of components to compile when this NgModule is defined, so that they can be dynamically loaded into the view. |
bootstrap? | The set of components that are bootstrapped when
this module is bootstrapped. The components listed here
are automatically added to |
schemas? | The set of schemas that declare elements to be allowed in the NgModule. Elements and properties that are neither Angular components nor directives must be declared in a schema. |
id? | A name or path that uniquely identifies this NgModule in getModuleFactory
If left |
jit? | When present, this module is ignored by the AOT compiler.
It remains in distributed code, and the JIT compiler attempts to compile it
at run time, in the browser.
To ensure the correct behavior, the app must import |
What are the lifecycle hooks in an Angular component
What is lazy loading in angular
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'products', loadChildren: () => import('./products/products.module').then(m => m.ProductsModule) },
// other routes...
];
const routes: Routes = [
{ path: '', component: ProductListComponent },
{ path: 'details/:id', component: ProductDetailsComponent },
// other routes specific to the feature module...
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ProductsRoutingModule { }
What are the ways to share data between 2 components
In Angular, there are several ways to share data between two components. Here are some common methods:
1. Parent-to-child communication (Input binding): In Angular, you can share data from a parent component to a child component by using input properties. The parent component binds a value to an input property of the child component. Example: Parent Component:
import { Component } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
`
})
export class ParentComponent {
sharedData: string = 'Hello from parent!';
}
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-child',
template: `
{{ data }}
`
})
export class ChildComponent {
@Input() data: string;
}
import { Component, EventEmitter, Output } from '@angular/core';
@Component({
selector: 'app-child',
template: `
Send Data
`
})
export class ChildComponent {
@Output() dataEvent = new EventEmitter();
sendData() {
const data = 'Hello from child!';
this.dataEvent.emit(data);
}
}
import { Component } from '@angular/core';
@Component({
selector: 'app-parent',
template: `
{{ receivedData }}
`
})
export class ParentComponent {
receivedData: string;
receiveData(data: string) {
this.receivedData = data;
}
}
import { Injectable } from '@angular/core';
@Injectable()
export class DataService {
sharedData: string = 'Hello from service!';
}
import { Component } from '@angular/core';
import { DataService } from 'path-to-data-service';
@Component({
selector: 'app-component-a',
template: `
{{ dataService.sharedData }}
`
})
export class ComponentA {
constructor(public dataService: DataService) {}
}
import { Component } from '@angular/core';
import { DataService } from 'path-to-data-service';
@Component({
selector: 'app-component-b',
template: `
Update Data
`
})
export class ComponentB {
constructor(public dataService: DataService) {}
updateData() {
this.dataService.sharedData = 'New data!';
}
}
What is a Single page application
Disadvantages of AOT
Don't:
export const couterReducer = (state, action: Action) => {
// ...
}
Do:
export function counterReducer(state, action: Action) {
// ...
}
Don't:
export default class AppComponent {};
Do:
export class AppComponent {};
-> form-control
Use this.helloForm.controls["greetingMessage"] to retrieve form control is fine.
Don't:
{{helloForm.controls["greetingMessage"].errors?.minlength}}
Do:
{{helloForm.controls["greetingMessage"].hasError("minlength")}}
Don't:
function random() {
return [{
path: "",
component: AppViewComponent
}];
}
const SAMPLE_APP_ROUTES: Routes = random();
Do:
const SAMPLE_APP_ROUTES: Routes = [{
path: "",
component: AppViewComponent
}];
Use of tilde(~) and caret(^) sign in package.json
"dependencies": {
"express": "^3.9.2"
}
"dependencies": {
"express": "~3.9.2"
}