Angular Flashcards

(66 cards)

1
Q

forkJoin

concatMap

mergeMap

switchMap

A

forkJoin -> parallel + wait for all to finish.

concatMap → sequential (one after the other).

mergeMap → parallel, but emits results as they arrive.

switchMap → cancels previous when a new one starts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between an attribute directive and a structural directive in Angular?

A

Attribute directives change the appearance or behavior of an existing element (e.g., changing style or adding classes). Structural directives change the DOM structure by adding or removing elements (e.g., *ngIf, *ngFor).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you create a custom attribute directive in Angular?

A

You create a directive class with the @Directive decorator and use HostBinding/HostListener to manipulate styles or behaviors. Example: highlight a button when it has a custom attribute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the core parts of directives in Angular?

A

The core parts are: the directive selector, injected dependencies via constructor, and HostBinding/HostListener to interact with DOM elements. Structural directives also use TemplateRef and ViewContainerRef to manage DOM elements dynamically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you show usage of a custom structural directive in Angular?

A

Yes. Example: *appIf condition, which internally uses TemplateRef and ViewContainerRef to conditionally add or remove elements from the DOM, similar to *ngIf.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are Angular Modules (NgModules)?

A

NgModules organize an Angular app into cohesive blocks of functionality. They are defined using @NgModule, which includes metadata such as declarations (components, directives, pipes), imports (other modules), providers (services), and exports (what’s made available outside the module).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the main purpose of Angular Modules?

A

To create reusable, encapsulated groups of code that structure large applications, manage dependencies clearly, and allow easy sharing of functionality across different parts of an app.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a directive in Angular?

A

A directive is a class that can modify the structure or behavior of DOM elements. There are three main types: Components, Attribute Directives, and Structural Directives.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the three types of directives in Angular?

A
  1. Components (template + behavior), 2. Attribute Directives (modify appearance/behavior), 3. Structural Directives (alter DOM structure, e.g., *ngIf, *ngFor).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the difference between attribute directives and structural directives?

A

Attribute directives change the appearance or behavior of elements (e.g., ngClass, ngStyle). Structural directives change the DOM layout by adding/removing elements (e.g., *ngIf, *ngFor).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How would you implement a custom attribute directive?

A

You create a directive class decorated with @Directive, inject ElementRef/Renderer2, and apply styles/behavior. Example: highlight button when attribute is applied.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you implement a custom structural directive?

A

You create a directive with @Directive, inject TemplateRef and ViewContainerRef, and use createEmbeddedView() or clear() to manipulate DOM structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the core building blocks of a directive?

A

Decorator (@Directive), Selector (CSS-like pattern), and Class with injected dependencies like ElementRef, Renderer2, TemplateRef, or ViewContainerRef.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can you show usage of a custom structural directive?

A

Yes. Example: *appUnless works like *ngIf but in reverse. Usage: <div *appUnless=’condition’>Shown if condition is false</div>.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is ngOnInit used for?

A

It is a lifecycle hook called after Angular initializes component inputs. It’s used for fetching data, initialization logic, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is ngOnDestroy used for?

A

It is a lifecycle hook used for cleanup, unsubscribing from observables, removing event listeners, and freeing resources.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is ngOnChanges used for?

A

It runs whenever input-bound properties change. Useful for reacting to @Input updates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is an Angular Module?

A

A logical grouping of Angular classes, components, directives, services, and other modules. Defined with @NgModule decorator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are the key metadata fields in @NgModule?

A

Declarations (components, directives, pipes), Imports (modules to use), Providers (services), and Exports (items made available to other modules).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is the purpose of declarations in an Angular module?

A

To declare components, directives, and pipes that belong to this module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is the purpose of imports in an Angular module?

A

To import external modules so their exported classes can be used inside this module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the purpose of providers in an Angular module?

A

To define services that should be available via dependency injection throughout the module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the purpose of exports in an Angular module?

A

To make components, directives, or pipes from this module available for use in other modules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the main use case of Angular modules?

A

To create reusable, cohesive groups of code that can be shared across an application.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How would you highlight a button using an attribute directive?
Create a custom directive with a selector like [appHighlight], inject ElementRef, and change element style. Usage: .
26
How does Angular differentiate between *ngIf and *ngFor?
Both are structural directives. *ngIf conditionally includes a template, *ngFor iterates over a collection and repeats a template.
27
Can we create multiple structural directives on one element?
No, you cannot apply more than one structural directive on the same host element. Instead, wrap elements or use .
28
What is the difference between constructor and ngOnInit in Angular?
Constructor is used to initialize services and other objects. ngOnInit is a lifecycle hook called after component inputs are initialized, suitable for performing logic that requires initialized inputs.
29
What is the purpose of @Input() in Angular?
It allows a parent component to pass data into a child component.
30
What is the purpose of @Output() in Angular?
It allows a child component to emit events to the parent component for communication.
31
What is the difference between ngIf and ngSwitch?
ngIf handles conditional rendering for usually one or two conditions (if/else). ngSwitch handles multiple conditions and selects one template based on a switch value.
32
What is change detection in Angular?
Change detection updates the view when data changes. Angular supports Default and OnPush strategies.
33
What is the difference between Default and OnPush change detection strategies?
Default checks all components on any change. OnPush checks only when input references change or events inside the component occur.
34
What is lazy loading in Angular?
Lazy loading delays the loading of a module until its route is accessed, reducing initial load time.
35
How does Angular handle dependency injection?
Angular uses constructor injection to provide services. Services can be provided at root, module, or component level, controlling their lifetime and scope.
36
What is the difference between providedIn root and providedIn child/module?
providedIn root creates a singleton available app-wide. Providing in a module or component scopes the service instance to that module/component.
37
What is an Observable in Angular?
Observable is a data stream that can be subscribed to for asynchronous operations like HTTP calls.
38
What is a Subject in RxJS?
Subject is a multicast Observable; multiple subscribers share the same data stream.
39
What is a BehaviorSubject?
A BehaviorSubject is a Subject that stores the last emitted value and emits it immediately to new subscribers.
40
What is a ReplaySubject?
A ReplaySubject stores the last n emitted values and replays them to new subscribers.
41
What is an AsyncSubject?
AsyncSubject emits only the last value of the source Observable, and only after it completes.
42
What is the difference between hot and cold observables?
Cold observables create a new stream for each subscription. Hot observables share a single stream across all subscribers.
43
What does the map operator do in RxJS?
It transforms emitted values from an observable into new values.
44
What does switchMap do?
switchMap maps each value to a new Observable and cancels the previous inner observable if a new value comes.
45
What does mergeMap do?
mergeMap maps each value to an Observable and subscribes to all inner Observables in parallel.
46
What does concatMap do?
concatMap maps each value to an Observable and subscribes sequentially, one after another.
47
What does exhaustMap do?
exhaustMap ignores new values while the previous inner observable is still active.
48
What is forkJoin?
forkJoin waits for all inner Observables to complete and emits their last values as an array.
49
What is combineLatest?
combineLatest emits values whenever any inner Observable emits, but only after all Observables have emitted at least once.
50
What is merge in RxJS?
merge emits values from multiple Observables as they occur, without waiting for completion.
51
What is zip in RxJS?
zip combines multiple Observables and emits arrays of values, taking one value from each Observable sequentially.
52
What does catchError do?
catchError handles errors in the observable stream and can return a new Observable.
53
What does retryWhen do?
retryWhen retries a failed observable based on a notifier Observable, often for implementing backoff logic.
54
What is the difference between reactive forms and template-driven forms?
Reactive forms are more declarative and defined in the component with FormControl/FormGroup. Template-driven forms are simpler and defined in HTML with ngModel.
55
What is a pipe in Angular?
A pipe transforms data for display in templates. Custom pipes extend PipeTransform and are declared in a module.
56
What is ViewChild used for?
ViewChild allows you to get a reference to a child component or DOM element in the component view.
57
What is ContentChild used for?
ContentChild allows you to get a reference to projected content inside a component using .
58
Which lifecycle hook is used to perform data fetching after a component is initialized?
ngOnInit
59
What lifecycle hook runs when input-bound properties change?
ngOnChanges
60
Which lifecycle hook runs on every change detection cycle?
ngDoCheck
61
Which lifecycle hook runs after the component view and its child views are initialized?
ngAfterViewInit
62
Which lifecycle hook is used for cleanup before the component is destroyed?
ngOnDestroy
63
What is the difference between ngOnInit() and the constructor in an Angular component? When should you use each?
The constructor is used for simple class initialization like injecting dependencies. It runs before any Angular lifecycle hooks and should not contain logic dependent on bindings. The **ngOnInit()** lifecycle hook is called after the component’s inputs (@Input() bindings) have been set. This is the proper place to fetch data, set up subscriptions, or perform logic that depends on input values.
64
What is the difference between @Input() and @Output() in Angular? When would you use each one?
@Input() allows a parent component to bind a value into a child component’s property. It’s used for data flow from parent to child. @Output() allows the child component to emit events to the parent using an EventEmitter. It supports event flow from child to parent, often for notifying the parent of user actions or internal state changes.
65
What is the difference between ngIf and ngSwitch in Angular? When would you choose one over the other?
*ngIf is used when you want to conditionally include or exclude a block of HTML based on a boolean expression. It supports optional else templates using the else keyword. *ngSwitch is used when you have multiple possible values for a single expression and want to render different templates depending on which one matches. It uses ngSwitchCase and ngSwitchDefault to handle various branches, similar to a switch statement in JavaScript.
66