diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml
new file mode 100644
index 0000000..0702a02
--- /dev/null
+++ b/.github/FUNDING.yml
@@ -0,0 +1,2 @@
+github: [sudheerj]
+custom: https://buymeacoffee.com/sudheerj
diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml
index 7c87150..8a32fac 100644
--- a/.github/workflows/main.yaml
+++ b/.github/workflows/main.yaml
@@ -16,12 +16,12 @@ jobs:
mkdir output # create output dir
- name: Create PDF
- uses: docker://pandoc/latex:2.9
+ uses: docker://pandoc/latex:2.10
with:
args: --pdf-engine=xelatex --output=output/${{env.FILE_NAME}}.pdf README.md
- name: Create epub
- uses: docker://pandoc/latex:2.9
+ uses: docker://pandoc/latex:2.10
with:
args: --output=output/${{env.FILE_NAME}}.epub README.md
diff --git a/LICENSE b/LICENSE
deleted file mode 100644
index 261eeb9..0000000
--- a/LICENSE
+++ /dev/null
@@ -1,201 +0,0 @@
- Apache License
- Version 2.0, January 2004
- http://www.apache.org/licenses/
-
- TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
-
- 1. Definitions.
-
- "License" shall mean the terms and conditions for use, reproduction,
- and distribution as defined by Sections 1 through 9 of this document.
-
- "Licensor" shall mean the copyright owner or entity authorized by
- the copyright owner that is granting the License.
-
- "Legal Entity" shall mean the union of the acting entity and all
- other entities that control, are controlled by, or are under common
- control with that entity. For the purposes of this definition,
- "control" means (i) the power, direct or indirect, to cause the
- direction or management of such entity, whether by contract or
- otherwise, or (ii) ownership of fifty percent (50%) or more of the
- outstanding shares, or (iii) beneficial ownership of such entity.
-
- "You" (or "Your") shall mean an individual or Legal Entity
- exercising permissions granted by this License.
-
- "Source" form shall mean the preferred form for making modifications,
- including but not limited to software source code, documentation
- source, and configuration files.
-
- "Object" form shall mean any form resulting from mechanical
- transformation or translation of a Source form, including but
- not limited to compiled object code, generated documentation,
- and conversions to other media types.
-
- "Work" shall mean the work of authorship, whether in Source or
- Object form, made available under the License, as indicated by a
- copyright notice that is included in or attached to the work
- (an example is provided in the Appendix below).
-
- "Derivative Works" shall mean any work, whether in Source or Object
- form, that is based on (or derived from) the Work and for which the
- editorial revisions, annotations, elaborations, or other modifications
- represent, as a whole, an original work of authorship. For the purposes
- of this License, Derivative Works shall not include works that remain
- separable from, or merely link (or bind by name) to the interfaces of,
- the Work and Derivative Works thereof.
-
- "Contribution" shall mean any work of authorship, including
- the original version of the Work and any modifications or additions
- to that Work or Derivative Works thereof, that is intentionally
- submitted to Licensor for inclusion in the Work by the copyright owner
- or by an individual or Legal Entity authorized to submit on behalf of
- the copyright owner. For the purposes of this definition, "submitted"
- means any form of electronic, verbal, or written communication sent
- to the Licensor or its representatives, including but not limited to
- communication on electronic mailing lists, source code control systems,
- and issue tracking systems that are managed by, or on behalf of, the
- Licensor for the purpose of discussing and improving the Work, but
- excluding communication that is conspicuously marked or otherwise
- designated in writing by the copyright owner as "Not a Contribution."
-
- "Contributor" shall mean Licensor and any individual or Legal Entity
- on behalf of whom a Contribution has been received by Licensor and
- subsequently incorporated within the Work.
-
- 2. Grant of Copyright License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- copyright license to reproduce, prepare Derivative Works of,
- publicly display, publicly perform, sublicense, and distribute the
- Work and such Derivative Works in Source or Object form.
-
- 3. Grant of Patent License. Subject to the terms and conditions of
- this License, each Contributor hereby grants to You a perpetual,
- worldwide, non-exclusive, no-charge, royalty-free, irrevocable
- (except as stated in this section) patent license to make, have made,
- use, offer to sell, sell, import, and otherwise transfer the Work,
- where such license applies only to those patent claims licensable
- by such Contributor that are necessarily infringed by their
- Contribution(s) alone or by combination of their Contribution(s)
- with the Work to which such Contribution(s) was submitted. If You
- institute patent litigation against any entity (including a
- cross-claim or counterclaim in a lawsuit) alleging that the Work
- or a Contribution incorporated within the Work constitutes direct
- or contributory patent infringement, then any patent licenses
- granted to You under this License for that Work shall terminate
- as of the date such litigation is filed.
-
- 4. Redistribution. You may reproduce and distribute copies of the
- Work or Derivative Works thereof in any medium, with or without
- modifications, and in Source or Object form, provided that You
- meet the following conditions:
-
- (a) You must give any other recipients of the Work or
- Derivative Works a copy of this License; and
-
- (b) You must cause any modified files to carry prominent notices
- stating that You changed the files; and
-
- (c) You must retain, in the Source form of any Derivative Works
- that You distribute, all copyright, patent, trademark, and
- attribution notices from the Source form of the Work,
- excluding those notices that do not pertain to any part of
- the Derivative Works; and
-
- (d) If the Work includes a "NOTICE" text file as part of its
- distribution, then any Derivative Works that You distribute must
- include a readable copy of the attribution notices contained
- within such NOTICE file, excluding those notices that do not
- pertain to any part of the Derivative Works, in at least one
- of the following places: within a NOTICE text file distributed
- as part of the Derivative Works; within the Source form or
- documentation, if provided along with the Derivative Works; or,
- within a display generated by the Derivative Works, if and
- wherever such third-party notices normally appear. The contents
- of the NOTICE file are for informational purposes only and
- do not modify the License. You may add Your own attribution
- notices within Derivative Works that You distribute, alongside
- or as an addendum to the NOTICE text from the Work, provided
- that such additional attribution notices cannot be construed
- as modifying the License.
-
- You may add Your own copyright statement to Your modifications and
- may provide additional or different license terms and conditions
- for use, reproduction, or distribution of Your modifications, or
- for any such Derivative Works as a whole, provided Your use,
- reproduction, and distribution of the Work otherwise complies with
- the conditions stated in this License.
-
- 5. Submission of Contributions. Unless You explicitly state otherwise,
- any Contribution intentionally submitted for inclusion in the Work
- by You to the Licensor shall be under the terms and conditions of
- this License, without any additional terms or conditions.
- Notwithstanding the above, nothing herein shall supersede or modify
- the terms of any separate license agreement you may have executed
- with Licensor regarding such Contributions.
-
- 6. Trademarks. This License does not grant permission to use the trade
- names, trademarks, service marks, or product names of the Licensor,
- except as required for reasonable and customary use in describing the
- origin of the Work and reproducing the content of the NOTICE file.
-
- 7. Disclaimer of Warranty. Unless required by applicable law or
- agreed to in writing, Licensor provides the Work (and each
- Contributor provides its Contributions) on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
- implied, including, without limitation, any warranties or conditions
- of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
- PARTICULAR PURPOSE. You are solely responsible for determining the
- appropriateness of using or redistributing the Work and assume any
- risks associated with Your exercise of permissions under this License.
-
- 8. Limitation of Liability. In no event and under no legal theory,
- whether in tort (including negligence), contract, or otherwise,
- unless required by applicable law (such as deliberate and grossly
- negligent acts) or agreed to in writing, shall any Contributor be
- liable to You for damages, including any direct, indirect, special,
- incidental, or consequential damages of any character arising as a
- result of this License or out of the use or inability to use the
- Work (including but not limited to damages for loss of goodwill,
- work stoppage, computer failure or malfunction, or any and all
- other commercial damages or losses), even if such Contributor
- has been advised of the possibility of such damages.
-
- 9. Accepting Warranty or Additional Liability. While redistributing
- the Work or Derivative Works thereof, You may choose to offer,
- and charge a fee for, acceptance of support, warranty, indemnity,
- or other liability obligations and/or rights consistent with this
- License. However, in accepting such obligations, You may act only
- on Your own behalf and on Your sole responsibility, not on behalf
- of any other Contributor, and only if You agree to indemnify,
- defend, and hold each Contributor harmless for any liability
- incurred by, or claims asserted against, such Contributor by reason
- of your accepting any such warranty or additional liability.
-
- END OF TERMS AND CONDITIONS
-
- APPENDIX: How to apply the Apache License to your work.
-
- To apply the Apache License to your work, attach the following
- boilerplate notice, with the fields enclosed by brackets "[]"
- replaced with your own identifying information. (Don't include
- the brackets!) The text should be enclosed in the appropriate
- comment syntax for the file format. We also recommend that a
- file or class name and description of purpose be included on the
- same "printed page" as the copyright notice for easier
- identification within third-party archives.
-
- Copyright [yyyy] [name of copyright owner]
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
diff --git a/README.md b/README.md
index 33629f7..abc3762 100644
--- a/README.md
+++ b/README.md
@@ -1,29 +1,23 @@
# Angular Interview Questions & Answers
-> Click :star:if you like the project. Pull Request are highly appreciated. Follow me [@SudheerJonna](https://twitter.com/SudheerJonna) for technical updates.
+> Click :star: if you like the project and follow [@SudheerJonna](https://twitter.com/SudheerJonna) for technical updates.
---
-
+
+
+
+
+
+
+ - Take this Angular course to go from a complete Angular beginner to confidently building enterprise-level applications from scratch
+ - Take this coding interview bootcamp if you're serious about getting hired and don't have a CS degree
+
+
+
---
-## Downloading PDF/Epub formats
-
-You can download the PDF and Epub version of this repository from the latest run on the [actions tab](https://github.com/sudheerj/angular-interview-questions/actions).
-
### Table of Contents
| No. | Questions |
@@ -74,11 +68,11 @@ You can download the PDF and Epub version of this repository from the latest run
|44| [What is the difference between promise and observable?](#what-is-the-difference-between-promise-and-observable)|
|45| [What is multicasting?](#what-is-multicasting)|
|46| [How do you perform error handling in observables?](#how-do-you-perform-error-handling-in-observables)|
-|47| [What is the short hand notation for subscribe method?](#what-is-the-short-hand-notation-for-subscribe-method)|
+|47| [What is the shorthand notation for subscribe method?](#what-is-the-shorthand-notation-for-subscribe-method)|
|48| [What are the utility functions provided by RxJS?](#what-are-the-utility-functions-provided-by-rxjs)|
|49| [What are observable creation functions?](#what-are-observable-creation-functions)|
-|50| [What will happen if you do not supply handler for observer?](#what-will-happen-if-you-do-not-supply-handler-for-observer)|
-|51| [What are angular elements?](#what-are-angular-elements)|
+|50| [What will happen if you do not supply handler for the observer?](#what-will-happen-if-you-do-not-supply-handler-for-the-observer)|
+|51| [What are Angular elements?](#what-are-angular-elements)|
|52| [What is the browser support of Angular Elements?](#what-is-the-browser-support-of-angular-elements)|
|53| [What are custom elements?](#what-are-custom-elements)|
|54| [Do I need to bootstrap custom elements?](#do-i-need-to-bootstrap-custom-elements)|
@@ -110,7 +104,7 @@ You can download the PDF and Epub version of this repository from the latest run
|80| [What are the advantages with AOT?](#what-are-the-advantages-with-aot)|
|81| [What are the ways to control AOT compilation?](#what-are-the-ways-to-control-aot-compilation)|
|82| [What are the restrictions of metadata?](#what-are-the-restrictions-of-metadata)|
-|83| [What are the two phases of AOT?](#what-are-the-two-phases-of-aot)|
+|83| [What are the three phases of AOT?](#what-are-the-three-phases-of-aot)|
|84| [Can I use arrow functions in AOT?](#can-i-use-arrow-functions-in-aot)|
|85| [What is the purpose of metadata json files?](#what-is-the-purpose-of-metadata-json-files)|
|86| [Can I use any javascript feature for expression syntax in AOT?](#can-i-use-any-javascript-feature-for-expression-syntax-in-aot)|
@@ -169,7 +163,7 @@ You can download the PDF and Epub version of this repository from the latest run
|139| [How do you detect route change in Angular?](#how-do-you-detect-route-change-in-angular)|
|140| [How do you pass headers for HTTP client?](#how-do-you-pass-headers-for-http-client)|
|141| [What is the purpose of differential loading in CLI?](#what-is-the-purpose-of-differential-loading-in-cli)|
-|142| [Is Angular supports dynamic imports?](#is-angular-supports-dynamic-imports)|
+|142| [Does Angular support dynamic imports?](#does-angular-support-dynamic-imports)|
|143| [What is lazy loading?](#what-is-lazy-loading)|
|144| [What are workspace APIs?](#what-are-workspace-apis)|
|145| [How do you upgrade angular version?](#how-do-you-upgrade-angular-version)|
@@ -192,19 +186,19 @@ You can download the PDF and Epub version of this repository from the latest run
|162| [What is Angular security model for preventing XSS attacks?](#what-is-angular-security-model-for-preventing-xss-attacks)|
|163| [What is the role of template compiler for prevention of XSS attacks?](#what-is-the-role-of-template-compiler-for-prevention-of-xss-attacks)|
|164| [What are the various security contexts in Angular?](#what-are-the-various-security-contexts-in-Angular)|
-|165| [What is Sanitization? Is angular supports it?](#what-is-sanitization?Is-angular-supports-it)|
+|165| [What is Sanitization? Does Angular support it?](#what-is-sanitization-does-angular-support-it)|
|166| [What is the purpose of innerHTML?](#what-is-the-purpose-of-innerhtml)|
|167| [What is the difference between interpolated content and innerHTML?](#what-is-the-difference-between-interpolated-content-and-innerhtml)|
|168| [How do you prevent automatic sanitization?](#how-do-you-prevent-automatic-sanitization)|
-|169| [Is safe to use direct DOM API methods in terms of security?](#is-safe-to-use-direct-dom-api-methods-in-terms-of-security)|
+|169| [Is it safe to use direct DOM API methods in terms of security?](#is-it-safe-to-use-direct-dom-api-methods-in-terms-of-security)|
|170| [What is DOM sanitizer?](#what-is-dom-sanitizer)|
|171| [How do you support server side XSS protection in Angular application?](#how-do-you-support-server-side-xss-protection-in-angular-application)
-|172| [Is angular prevents http level vulnerabilities?](#is-angular-prevents-http-level-vulnerabilities)|
+|172| [Does Angular prevent HTTP level vulnerabilities?](#does-angular-prevent-http-level-vulnerabilities)|
|173| [What are Http Interceptors?](#what-are-http-interceptors)|
|174| [What are the applications of HTTP interceptors?](#what-are-the-applications-of-http-interceptors)|
-|175| [Is multiple interceptors supported in Angular?](#is-multiple-interceptors-supported-in-angular)|
+|175| [Are multiple interceptors supported in Angular?](#are-multiple-interceptors-supported-in-angular)|
|176| [How can I use interceptor for an entire application?](#how-can-i-use-interceptor-for-an-entire-application)|
-|177| [How does Angular simplifies Internationalization?](#how-does-angular-simplifies-internationalization)|
+|177| [How does Angular simplify Internationalization?](#how-does-angular-simplify-internationalization)|
|178| [How do you manually register locale data?](#how-do-you-manually-register-locale-data)|
|179| [What are the four phases of template translation?](#what-are-the-four-phases-of-template-translation)|
|180| [What is the purpose of i18n attribute?](#what-is-the-purpose-of-i18n-attribute)|
@@ -246,7 +240,7 @@ You can download the PDF and Epub version of this repository from the latest run
|216| [Is it necessary for bootstrapped component to be entry component?](#is-it-necessary-for-bootstrapped-component-to-be-entry-component)|
|217| [What is a routed entry component?](#what-is-a-routed-entry-component#)|
|218| [Why is not necessary to use entryComponents array every time?](#why-is-not-necessary-to-use-entrycomponents-array-every-time)|
-|219| [Do I still need to use entryComponents array in Angular9?](do-i-still-need-to-use-entrycomponents-array-in-angular9#)|
+|219| [Do I still need to use entryComponents array in Angular9?](#do-i-still-need-to-use-entrycomponents-array-in-angular9#)|
|220| [Is it all components generated in production build?](#is-it-all-components-generated-in-production-build)|
|221| [What is Angular compiler?](#what-is-angular-compiler)|
|222| [What is the role of ngModule metadata in compilation process?](#what-is-the-role-of-ngmodule-metadata-in-compilation-process)|
@@ -270,7 +264,7 @@ You can download the PDF and Epub version of this repository from the latest run
|240| [How do you get current direction for locales??](#how-do-you-get-current-direction-for-locales)|
|241| [What is ngcc?](#what-is-ngcc)|
|242| [What classes should not be added to declarations?](#what-classes-should-not-be-added-to-declarations)|
-|243| [Wat is ngzone?](#what-is-ngzone)|
+|243| [What is ngzone?](#what-is-ngzone)|
|244| [What is NoopZone?](#what-is-noopzone)|
|245| [How do you create displayBlock components?](#how-do-you-create-displayblock-components)|
|246| [What are the possible data change scenarios for change detection?](#what-are-the-possible-data-change-scenarios-for-change-detection)|
@@ -299,40 +293,47 @@ You can download the PDF and Epub version of this repository from the latest run
|269| [How to set ngFor and ngIf on the same element?](#how-to-set-ngfor-and-ngif-on-the-same-element)|
|270| [What is host property in css?](#what-is-host-property-in-css)|
|271| [How do you get the current route?](#how-do-you-get-the-current-route)|
-|272| [](#)|
-|273| [](#)|
-|274| [](#)|
-|275| [](#)|
-|276| [](#)|
+|272| [What is Component Test Harnesses?](#what-is-component-test-harnesses)|
+|273| [What is the benefit of Automatic Inlining of Fonts?](#what-is-the-benefit-of-automatic-inlining-of-fonts)|
+|274| [What is content projection?](#what-is-content-projection)|
+|275| [What is ng-content and its purpose?](#what-is-ng-content-and-its-purpose)|
+|276| [What is standalone component?](#what-is-standalone-component)|
+|277| [How to create a standalone component uing CLI command?](#how-to-create-a-standalone-component-uing-cli-command)
+|278| [How to create a standalone component manually?](#how-to-create-a-standalone-component-manually)
+|279| [What is hydration ?](#what-is-hydration)
+|280| [What are Angular Signals?](#what-are-angular-signals)
+|281| [Explain Angular Signals with an example](#explain-angular-signals-with-an-example)
+|282| [What are the Route Parameters? Could you explain each of them?](#what-are-the-route-parameters-could-you-explain-each-of-them)
+|283| [](#)
1. ### What is Angular Framework?
- Angular is a **TypeScript-based open-source** front-end platform that makes it easy to build applications with in web/mobile/desktop. The major features of this framework such as declarative templates, dependency injection, end to end tooling, and many more other features are used to ease the development.
+ Angular is a **TypeScript-based open-source** front-end platform that makes it easy to build web, mobile and desktop applications. The major features of this framework include declarative templates, dependency injection, end to end tooling which ease application development.
**[⬆ Back to Top](#table-of-contents)**
2. ### What is the difference between AngularJS and Angular?
- Angular is a completely revived component-based framework in which an application is a tree of individual components.
+ AngularJS (version 1.x) is a JavaScript framework, whereas Angular (version 2+) is a complete rewrite of AngularJS using TypeScript, providing better performance, mobile support, modularity, and a more modern architecture.
- Some of the major difference in tabular form
+ Here are some of the major differences in tabular format:-
| AngularJS | Angular |
|---- | ---------
- | It is based on MVC architecture | This is based on Service/Controller |
- | This uses use JavaScript to build the application| Introduced the typescript to write the application |
+ | It is based on MVC architecture| This is based on Service/Controller|
+ | It uses JavaScript to build the application| Uses TypeScript to build the application|
| Based on controllers concept| This is a component based UI approach|
- | Not a mobile friendly framework| Developed considering mobile platform|
- | Difficulty in SEO friendly application development| Ease to create SEO friendly applications|
+ | No support for mobile platforms| Fully supports mobile platforms|
+ | Difficult to build SEO friendly application| Ease to build SEO friendly applications|
**[⬆ Back to Top](#table-of-contents)**
3. ### What is TypeScript?
- TypeScript is a typed superset of JavaScript created by Microsoft that adds optional types, classes, async/await, and many other features, and compiles to plain JavaScript. Angular built entirely in TypeScript and used as a primary language.
- You can install it globally as
+ TypeScript is a strongly typed superset of JavaScript created by Microsoft that adds optional types, classes, async/await and many other features, and compiles to plain JavaScript. Angular is written entirely in TypeScript as a primary language.
+ You can install TypeScript globally as
```cmd
npm install -g typescript
```
- Let's see a simple example of TypeScript usage,
+ Let's see a simple example of TypeScript usage:-
```typescript
function greeter(person: string) {
return "Hello, " + person;
@@ -347,17 +348,17 @@ You can download the PDF and Epub version of this repository from the latest run
**[⬆ Back to Top](#table-of-contents)**
4. ### Write a pictorial diagram of Angular architecture?
- The main building blocks of an Angular application is shown in the below diagram
+ The main building blocks of an Angular application are shown in the diagram below:-

**[⬆ Back to Top](#table-of-contents)**
5. ### What are the key components of Angular?
- Angular has the below key components,
- 1. **Component:** These are the basic building blocks of angular application to control HTML views.
- 2. **Modules:** An angular module is set of angular basic building blocks like component, directives, services etc. An application is divided into logical pieces and each piece of code is called as "module" which perform a single task.
- 3. **Templates:** This represent the views of an Angular application.
- 4. **Services:** It is used to create components which can be shared across the entire application.
+ Angular has the key components below,
+ 1. **Component:** These are the basic building blocks of an Angular application to control HTML views.
+ 2. **Modules:** An Angular module is a set of angular basic building blocks like components, directives, services etc. An application is divided into logical pieces and each piece of code is called as "module" which perform a single task.
+ 3. **Templates:** These represent the views of an Angular application.
+ 4. **Services:** Are used to create components which can be shared across the entire application.
5. **Metadata:** This can be used to add more data to an Angular class.
**[⬆ Back to Top](#table-of-contents)**
@@ -382,7 +383,7 @@ You can download the PDF and Epub version of this repository from the latest run
**[⬆ Back to Top](#table-of-contents)**
7. ### What are components?
- Components are the most basic UI building block of an Angular app which formed a tree of Angular components. These components are subset of directives. Unlike directives, components always have a template and only one component can be instantiated per an element in a template.
+ Components are the most basic UI building block of an Angular app, which form a tree of Angular components. These components are a subset of directives. Unlike directives, components always have a template, and only one component can be instantiated per element in a template.
Let's see a simple example of Angular component
```typescript
import { Component } from '@angular/core';
@@ -409,9 +410,9 @@ You can download the PDF and Epub version of this repository from the latest run
| Component | Directive |
|---- | ---------
- | To register a component we use @Component meta-data annotation | To register directives we use @Directive meta-data annotation |
- | Components are typically used to create UI widgets| Directive is used to add behavior to an existing DOM element |
- | Component is used to break up the application into smaller components| Directive is use to design re-usable components|
+ | To register a component we use @Component meta-data annotation | To register a directive we use @Directive meta-data annotation |
+ | Components are typically used to create UI widgets| Directives are used to add behavior to an existing DOM element |
+ | Component is used to break down the application into smaller components| Directive is used to design re-usable components|
| Only one component can be present per DOM element | Many directives can be used per DOM element |
| @View decorator or templateurl/template are mandatory | Directive doesn't use View|
@@ -471,11 +472,11 @@ You can download the PDF and Epub version of this repository from the latest run
})
export class AppModule { }
```
- The NgModule decorator has five important(among all) options
- 1. The imports option is used to import other dependent modules. The BrowserModule is required by default for any web based angular application
- 2. The declarations option is used to define components in the respective module
- 3. The bootstrap option tells Angular which Component to bootstrap in the application
- 4. The providers option is used to configure set of injectable objects that are available in the injector of this module.
+ The NgModule decorator has five important (among all) options:
+ 1. The imports option is used to import other dependent modules. The BrowserModule is required by default for any web based angular application.
+ 2. The declarations option is used to define components in the respective module.
+ 3. The bootstrap option tells Angular which Component to bootstrap in the application.
+ 4. The providers option is used to configure a set of injectable objects that are available in the injector of this module.
5. The entryComponents option is a set of components dynamically loaded into the view.
**[⬆ Back to Top](#table-of-contents)**
@@ -579,7 +580,7 @@ You can download the PDF and Epub version of this repository from the latest run
}
}
```
- 4. **Parameter decorators** Used for parameters inside class constructors, e.g. @Inject, Optional
+ 4. **Parameter decorators** Used for parameters inside class constructors, e.g. @Inject, @Optional
```typescript
import { Component, Inject } from '@angular/core';
import { MyService } from './my-service';
@@ -593,6 +594,22 @@ You can download the PDF and Epub version of this repository from the latest run
console.log(myService); // MyService
}
}
+
+ // Angular v18 and above
+
+ import { Component, inject } from '@angular/core';
+ import { MyService } from './my-service';
+
+ @Component({
+ selector: 'my-component',
+ template: 'Parameter decorator
'
+ })
+ export class MyComponent {
+ myService: MyService = inject(MyService)
+ constructor() {
+ console.log(myService); // MyService
+ }
+ }
```
**[⬆ Back to Top](#table-of-contents)**
@@ -620,17 +637,20 @@ You can download the PDF and Epub version of this repository from the latest run
**[⬆ Back to Top](#table-of-contents)**
15. ### What is the difference between constructor and ngOnInit?
- TypeScript classes has a default method called constructor which is normally used for the initialization purpose. Whereas ngOnInit method is specific to Angular, especially used to define Angular bindings. Even though constructor getting called first, it is preferred to move all of your Angular bindings to ngOnInit method.
- In order to use ngOnInit, you need to implement OnInit interface as below,
+ The **Constructor** is a default method of the class that is executed when the class is instantiated and ensures proper initialisation of fields in the class and its subclasses. Angular, or better Dependency Injector (DI), analyses the constructor parameters and when it creates a new instance by calling new MyClass() it tries to find providers that match the types of the constructor parameters, resolves them and passes them to the constructor.
+ **ngOnInit** is a life cycle hook called by Angular to indicate that Angular is done creating the component.
+ Mostly we use ngOnInit for all the initialization/declaration and avoid stuff to work in the constructor. The constructor should only be used to initialize class members but shouldn't do actual "work".
+ So you should use constructor() to setup Dependency Injection and not much else. ngOnInit() is better place to "start" - it's where/when components' bindings are resolved.
```typescript
export class App implements OnInit{
- constructor(){
+ constructor(private myService: MyService){
//called first time before the ngOnInit()
}
ngOnInit(){
//called after the constructor and called after the first ngOnChanges()
+ //e.g. http call...
}
}
```
@@ -669,6 +689,29 @@ You can download the PDF and Epub version of this repository from the latest run
**[⬆ Back to Top](#table-of-contents)**
18. ### How is Dependency Hierarchy formed?
+ Injectors in Angular have rules that can be leveraged to achieve the desired visibility of injectables in your applications. By understanding these rules, you can determine in which NgModule, Component, or Directive you should declare a provider.
+
+ #### Angular has two injector hierarchies:
+ 
+
+ #### Module injector
+ When angular starts, it creates a root injector where the services will be registered, these are provided via injectable annotation. All services provided in the `ng-model` property are called providers (if those modules are not lazy-loaded).
+
+ Angular recursively goes through all models which are being used in the application and creates instances for provided services in the root injector. If you provide some service in an eagerly-loaded model, the service will be added to the root injector, which makes it available across the whole application.
+
+ #### Platform Module
+ During application bootstrapping angular creates a few more injectors, above the root injector goes the platform injector, this one is created by the platform browser dynamic function inside the `main.ts` file, and it provides some platform-specific features like `DomSanitizer`.
+
+ #### NullInjector()
+ At the very top, the next parent injector in the hierarchy is the `NullInjector()`.The responsibility of this injector is to throw the error if something tries to find dependencies there, unless you've used `@Optional()` because ultimately, everything ends at the `NullInjector()` and it returns an error or, in the case of `@Optional()`, `null`.
+
+ 
+
+
+ #### ElementInjector
+ Angular creates `ElementInjector` hierarchies implicitly for each DOM element. `ElementInjector` injector is being created for any tag that matches the angular component, or any tag on which directive is applied, and you can configure it in component and directive annotations inside the provider's property, thus, it creates its own hierarchy likewise the upper one.
+
+ 
**[⬆ Back to Top](#table-of-contents)**
@@ -683,9 +726,14 @@ You can download the PDF and Epub version of this repository from the latest run
Time: {{ time | async }}`
})
export class AsyncObservablePipeComponent {
- time = new Observable(observer =>
- setInterval(() => observer.next(new Date().toString()), 2000)
- );
+ time: Observable;
+ constructor() {
+ this.time = new Observable((observer) => {
+ setInterval(() => {
+ observer.next(new Date().toString());
+ }, 2000);
+ });
+ }
}
```
@@ -701,19 +749,19 @@ You can download the PDF and Epub version of this repository from the latest run
**[⬆ Back to Top](#table-of-contents)**
-21. ### What is the purpose of ngFor directive?
- We use Angular ngFor directive in the template to display each item in the list. For example, here we iterate over list of users,
+21. ### What is the purpose of `*ngFor` directive?
+ We use Angular `*ngFor` directive in the template to display each item in the list. For example, here we can iterate over a list of users:
```html
{{ user }}
```
- The user variable in the ngFor double-quoted instruction is a **template input variable**
+ The user variable in the `*ngFor` double-quoted instruction is a **template input variable**.
**[⬆ Back to Top](#table-of-contents)**
-22. ### What is the purpose of ngIf directive?
- Sometimes an app needs to display a view or a portion of a view only under specific circumstances. The Angular ngIf directive inserts or removes an element based on a truthy/falsy condition. Let's take an example to display a message if the user age is more than 18,
+22. ### What is the purpose of `*ngIf` directive?
+ Sometimes an app needs to display a view or a portion of a view only under specific circumstances. The Angular `*ngIf` directive inserts or removes an element based on a truthy/falsy condition. Let's take an example to display a message if the user age is more than 18:
```html
18">You are not eligible for student pass!
```
@@ -723,7 +771,7 @@ You can download the PDF and Epub version of this repository from the latest run
23. ### What happens if you use script tag inside template?
- Angular recognizes the value as unsafe and automatically sanitizes it, which removes the **