In Angular, components often need to communicate with each other to share data and respond to events. There are several methods to achieve this, depending on the relationship between the components and the nature of the data being shared.
Parent to Child Communication
Using @Input Decorator: The parent component can pass data to the child component using the @Input decorator. This allows the parent to bind a property to the child component's input property.
Child to Parent Communication
Using @Output and EventEmitter: The child component can emit events to the parent component using the @Output decorator and EventEmitter. This allows the child to notify the parent about changes or actions.
Sibling Communication or Communication Without Direct Relationship
Using a Service with Observables: When components do not have a direct parent-child relationship, a shared service can be used to facilitate communication. The service can use RxJS Subject or BehaviorSubject to emit and subscribe to data changes.
These methods provide a robust way to handle communication between Angular components, ensuring data is passed efficiently and events are handled appropriately
Here are 50 good and basic Angular interview questions with answers, useful for beginners to intermediate developers (0–5 years experience):
🔹 Angular Basics (1–15)
-
What is Angular?
Angular is a TypeScript-based open-source web application framework developed by Google for building dynamic single-page applications (SPAs). -
What are the key features of Angular?
-
Component-based architecture
-
Two-way data binding
-
Dependency Injection
-
Directives
-
Routing
-
Services
-
RxJS & Observables
-
-
What is a Component in Angular?
A component controls a patch of the screen called a view. It contains a TypeScript class, HTML template, and optional CSS styles. -
What is a Module in Angular?
A module (NgModule) is a container that groups components, services, pipes, and other modules. -
What is the difference between
ngOnInit()and constructor()?-
constructor()is used to inject dependencies. -
ngOnInit()is a lifecycle hook that runs after component initialization.
-
-
What is Data Binding?
It’s a mechanism to bind data between component class and template.
Types:-
Interpolation (
{{ }}) -
Property binding (
[property]) -
Event binding (
(event)) -
Two-way binding (
[(ngModel)])
-
-
What is the difference between one-way and two-way data binding?
-
One-way: data flows from model to view or view to model.
-
Two-way: data flows in both directions.
-
-
What are Directives in Angular?
Directives are used to manipulate the DOM.-
Structural:
*ngIf,*ngFor -
Attribute:
[ngClass],[ngStyle] -
Custom Directives
-
-
What is a Service in Angular?
Services are used to write business logic or reusable code. They can be injected using dependency injection. -
What is Dependency Injection (DI)?
A design pattern where objects are passed to a class rather than created by it. Angular provides built-in DI. -
What is Routing in Angular?
Routing allows navigation between components using URLs. -
What is
@Input()and@Output()?-
@Input()allows data to be passed from parent to child. -
@Output()allows child to emit events to parent usingEventEmitter.
-
-
What is ngModel?
It enables two-way data binding between the input field and the component class. -
What is a Pipe in Angular?
Pipes are used to transform data in templates, e.g.,{{ price | currency }}. -
What is an Observable?
An observable is a stream of data that can be observed asynchronously (provided by RxJS).
🔹 Intermediate Concepts (16–30)
-
What is the difference between
ngIfandngSwitch?-
ngIf: conditionally includes a template. -
ngSwitch: displays one among many elements based on matching value.
-
-
What is the use of
ngFor?
It’s a structural directive used to loop over a list and render HTML for each item. -
What are Lifecycle Hooks in Angular?
Functions triggered during different stages of a component’s life:-
ngOnInit() -
ngOnDestroy() -
ngAfterViewInit()etc.
-
-
What is Lazy Loading in Angular?
A technique to load feature modules only when needed, improving performance. -
How to create a component in Angular?
Using CLI: -
What is the Angular CLI?
Command Line Interface to generate, build, test, and serve Angular applications. -
What is
HttpClientModule?
It allows HTTP communication (GET, POST, etc.) with backend services. -
What is
FormGroupandFormControl?
They are used for reactive forms in Angular to handle form inputs and validations. -
What is the difference between Template-driven and Reactive forms?
-
Template-driven: simple, based on template
-
Reactive: uses FormGroup/FormControl, more scalable
-
-
What is
ng-content?
Allows projection of content into a component from its parent. -
What is
ChangeDetectionStrategy?
It controls when Angular checks for changes.-
Default
-
OnPush (optimized)
-
-
What is
trackByinngFor?
Improves performance by tracking items uniquely by ID or key during re-render. -
What is
Renderer2?
A service to safely modify DOM elements (avoids direct DOM manipulation). -
What is the purpose of
Zone.jsin Angular?
Tracks async operations and triggers change detection automatically. -
What are Guards in Angular?
-
Route protection features:
-
CanActivate,CanDeactivate,Resolve, etc.
-
-
🔹 Advanced/Best Practices (31–50)
-
What is a Singleton Service?
A service with one instance throughout the application. Provided inroot. -
What is Ahead-of-Time (AOT) Compilation?
Pre-compiles Angular HTML and TypeScript code before browser loads it, improving performance. -
What is the difference between AOT and JIT?
-
AOT: Compile at build time
-
JIT: Compile at runtime
-
-
What is Tree Shaking?
Removes unused code during build to reduce bundle size. -
What is
ngZone.run()used for?
Forces Angular to run change detection manually for external async operations. -
What are Interceptors in Angular?
Middleware for HTTP requests. Used to add headers, handle errors, show loaders, etc. -
What is a Resolver in Angular?
Pre-fetches data before a route is activated. -
What is SSR (Server-Side Rendering)?
Rendering Angular application on the server for faster first load (Angular Universal). -
How to share data between unrelated components?
-
Using shared service with Subject/BehaviorSubject
-
State management (NgRx)
-
-
What is
@HostListener()?
Listens to events on the host element or document/window. -
What is
@ViewChild()?
Accesses a child component, directive, or DOM element from the parent component. -
What is
ng-template?
Defines an Angular template that isn’t rendered until called explicitly. -
What is
asyncpipe?
Automatically subscribes and unsubscribes from Observables in templates. -
What is
SubjectvsBehaviorSubjectin RxJS?-
Subject: emits to current subscribers only -
BehaviorSubject: remembers the last value and emits it to new subscribers
-
-
What is
ngAfterViewInit()lifecycle hook?
Called after the component’s view and child views are initialized. -
What is a Pure Pipe?
Executes only when input data changes. Improves performance. -
What is a Custom Directive?
You can create your own directive to add behavior to DOM elements. -
What is the role of
environment.ts?
Stores environment-specific settings like API URLs, build type, etc. -
What is Ivy in Angular?
A new rendering engine (from Angular 9+) with improved performance and smaller bundle sizes. -
What is
ng-contentvsContentChildvsViewChild? -
ng-content: content projection -
ContentChild: access projected content -
ViewChild: access view DOM or child components
No comments:
Post a Comment