6. What is an AOT compilation? What are its benefits?#
AOT compilation is an acronym for “Ahead-of-Time” compilation. It is a process that translates the application's HTML and TypeScript code into optimized JavaScript code during the build phase before the application is loaded in the browser. The result is easy to execute efficient and faster compiled code, enabling an enhanced user experience.
Some of the benefits of AOT compilation in Angular are:
Improved performance: Since the app is compiled ahead of time, the browser can directly execute the optimized JavaScript code without needing to compile the TypeScript at runtime. This results in faster load times and better overall performance compared to Just-in-Time (JIT) compilation.
Smaller bundle size: AOT eliminates unnecessary code, metadata, and unused features from the application during the build process, reducing the JavaScript bundle size. This leads to quicker downloads, faster loading, and reduced data usage.
Enhanced security: With AOT, the compilation happens during the build, meaning there is no need for a runtime compiler in the browser. This reduces the surface area for security vulnerabilities, such as injection attacks, as there’s no dynamic code evaluation at runtime.
Early error detection: AOT compilation allows for catching template errors and other issues at build time rather than at runtime. This helps developers address issues before deploying the app, resulting in fewer runtime errors and easier debugging.
7. Define life cycle hooks in Angular with examples#
Life cycle hooks are predefined methods in Angular that enable developers to access different stages of a component or a directive’s life cycle. This life cycle begins with the instantiation and rendering of the view and ends with the destruction and removal of the component or directive from the DOM.
Angular performs change detection throughout the life cycle to update the view and component instance whenever necessary. Developers can utilize life cycle hooks to execute custom code at specific stages, allowing greater control over the functionality and behavior of their components or directives.
Some examples of life cycle hooks are:
We can use ngOnChanges
to react to changes in @Input
properties.
Fetching data from a service once the component is initialized.
Use ngDoCheck
to check for changes that Angular’s default change detection might not catch.
ngAfterContentInit
: We invoke this method only after the component's content has been projected into its view.
ngAfterContentChecked
: We invoke this method after the component’s content has been checked for changes.
ngAfterViewInit
: This method is called after we verify that the component’s view has been initialized.
ngAfterViewChecked
: This method is called after the component’s view has been checked for changes.
ngOnDestroy
: This method is invoked before the component’s destruction. It enables you to perform any necessary cleanup logic.
8. What is the difference between a constructor and ngOnInit in Angular?#
A constructor is a special method in Angular that is called whenever a component is created. It’s part of the TypeScript class definition and is used for initializing the class properties and dependencies. ngOnInit
, on the other hand, is a life cycle hook called by Angular after initializing the component and its inputs have been set.
The key distinction between the two is that ngOnInit
is triggered after Angular initializes the component. ngOnInit
is also where the actual business logic (complex initialization) is performed as opposed to the constructor.
Additionally, the constructor is only called once during component creation, while ngOnInit
is also called once during the component life cycle—not multiple times. Other life cycle hooks like ngOnChanges
handle input changes, not ngOnInit
.
9. How would you define a single-page application (SPA)?#
A single-page application (SPA) is a web application that dynamically updates the current web page without requiring a full page reload. We create a SPA in Angular using the Angular framework. It consists of a single HTML page that is dynamically updated with new data from the server as the user engages with the system.
This approach ensures a seamless and smooth user experience akin to a desktop application. The navigation and data exchange between components occur without page reloading, translating into faster and more responsive web applications.
10. What are annotations?#
Annotations are ways of adding metadata to a class or its members using decorators. The decorators are a TypeScript feature—the language used to write Angular applications.
Annotations are specifically used to supply information to the Angular compiler on how to process a class and/or its members. We may use them to specify various things, such as the class’s dependencies, how it should be injected, and how it can be used in templates.
To add annotations to classes, methods, and properties, we use the @
symbol followed by the decorator name. For instance: