Questions II Flashcards
What is dependency injection in Angular?
Dependency injection (DI), is an important application design pattern in which a class asks for dependencies from external sources rather than creating them itself. Angular comes with its own dependency injection framework for resolving dependencies( services or objects that a class needs to perform its function).So you can have your services depend on other services throughout your application.
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.
What happens if you use script tag inside template?
Angular recognizes the value as unsafe and automatically sanitizes it, which removes the script tag but keeps safe content such as the text content of the script tag. This way it eliminates the risk of script injection attacks. If you still use it then it will be ignored and a warning appears in the browser console.
What is interpolation?
Interpolation is a special syntax that Angular converts into property binding. It’s a convenient alternative to property binding. It is represented by double curly braces({{}}). The text between the braces is often the name of a component property. Angular replaces that name with the string value of the corresponding component property.
What is a parameterized pipe?
pipe can accept any number of optional parameters to fine-tune its output. The parameterized pipe can be created by declaring the pipe name with a colon ( : ) and then the parameter value. If the pipe accepts multiple parameters, separate the values with colons. Let’s take a birthday example with a particular format(dd/MM/yyyy):
A parameterized pipe in Angular is a pipe that accepts additional parameters to transform the data in a specific way
How do you chain pipes?
You can chain pipes together in potentially useful combinations as per the needs.
What is the difference between pure and impure pipe?
A pure pipe is only called when Angular detects a change in the value or the parameters passed to a pipe. For example, any changes to a primitive input value (String, Number, Boolean, Symbol) or a changed object reference (Date, Array, Function, Object).
An impure pipe is called for every change detection cycle no matter whether the value or parameters changes. i.e, An impure pipe is called often, as often as every keystroke or mouse-move
What is a bootstrapping module?
Every application has at least one Angular module, the root module that you bootstrap to launch the application is called as bootstrapping module. It is commonly known as AppModule. The default structure of AppModule generated by AngularCLI would be as follows:
What is HttpClient and its benefits?
Most of the Front-end applications communicate with backend services over HTTP protocol using either XMLHttpRequest interface or the fetch() API. Angular provides a simplified client HTTP API known as HttpClient which is based on top of XMLHttpRequest interface. This client is available from @angular/common/http package.
The major advantages of HttpClient can be listed as below,
Contains testability features
Provides typed request and response objects
Intercept request and response
Supports Observable APIs
Supports streamlined error handling
What is RxJS?
RxJS is a library for composing asynchronous and callback-based code in a functional, reactive style using Observables. Many APIs such as HttpClient produce and consume RxJS Observables and also uses operators for processing observables.
What is subscribing?
An Observable instance begins publishing values only when someone subscribes to it. So you need to subscribe by calling the subscribe() method of the instance, passing an observer object to receive the notifications.
Let’s take an example of creating and subscribing to a simple observable, with an observer that logs the received message to the console.
What is an observable?
An Observable is a unique Object similar to a Promise that can help manage async code.
Observables are not part of the JavaScript language so we need to rely on a popular Observable library called RxJS. The observables are created using new keyword.
What is an observer?
Observer is an interface for a consumer of push-based notifications delivered by an Observable. It has below structure,
What is the difference between promise and observable- Promise
Executes immediately on creation
Provides only one value
Push errors to the child promises
Uses only .then() clause
Dont have operators
What is the difference between promise and observable- Observables
Declarative: Computation does not start until subscription, so they can run whenever you need the result
Provides multiple values over time
Subscribe method is used for error handling that facilitates centralized and predictable error handling
Provides chaining and subscription to handle complex applications
What is multicasting?
Multi-casting is the practice of broadcasting to a list of multiple subscribers in a single execution.
How do you perform error handling in observables?
You can handle errors by specifying an error callback on the observer instead of relying on try/catch, which are ineffective in asynchronous environment.
ERROR CALLBAC INSTEAD OD TRY/CATCH
What is the shorthand notation for subscribe method?
The subscribe() method can accept callback function definitions in line, for next, error, and complete handlers. It is known as shorthand notation or Subscribe method with positional arguments.
What are the utility functions provided by RxJS?
The RxJS library also provides below utility functions for creating and working with observables.
Converting existing code for async operations into observables
Iterating through the values in a stream
Mapping values to different types
Filtering streams
Composing multiple streams