Questions II Flashcards

1
Q

What is dependency injection in Angular?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How is Dependency Hierarchy formed?

A

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.

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

What happens if you use script tag inside template?

A

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.

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

What is interpolation?

A

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.

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

What is a parameterized pipe?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you chain pipes?

A

You can chain pipes together in potentially useful combinations as per the needs.

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

What is the difference between pure and impure pipe?

A

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

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

What is a bootstrapping module?

A

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:

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

What is HttpClient and its benefits?

A

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

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

What is RxJS?

A

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.

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

What is subscribing?

A

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.

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

What is an observable?

A

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.

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

What is an observer?

A

Observer is an interface for a consumer of push-based notifications delivered by an Observable. It has below structure,

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

What is the difference between promise and observable- Promise

A

Executes immediately on creation

Provides only one value

Push errors to the child promises

Uses only .then() clause

Dont have operators

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

What is the difference between promise and observable- Observables

A

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

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

What is multicasting?

A

Multi-casting is the practice of broadcasting to a list of multiple subscribers in a single execution.

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

How do you perform error handling in observables?

A

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

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

What is the shorthand notation for subscribe method?

A

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.

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

What are the utility functions provided by RxJS?

A

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

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

What are Angular elements?

A

Angular elements are Angular components packaged as custom elements (a web standard for defining new HTML elements in a framework-agnostic way). Angular Elements host an Angular component, providing a bridge between the data and the logic defined in the component and the standard DOM APIs, thus, providing a way to use Angular components in non-Angular environments.

21
Q

What are the various kinds of directives?

A

There are mainly three kinds of directives:

-> Components — These are directives with a template.

-> Structural directives — These directives change the DOM layout by adding and removing DOM elements.

ngIf, ngFor, ngSwitch

-> Attribute directives — These directives change the appearance or behavior of an element, component, or another directive.

ngClass, ngStyle

22
Q

How do you create directives using CLI?

A

You can use CLI command ng generate directive to create the directive class file. It creates the source file(src/app/components/directivename.directive.ts), the respective test file .spec.ts and declare the directive class file in root module.

23
Q

What is Angular Router?

A

Angular Router is a mechanism in which navigation happens from one view to the next as users perform application tasks. It borrows the concepts or model of browser’s application navigation. It enables developers to build Single Page Applications with multiple views and allow navigation between these views.

24
Q

What is the purpose of base href tag?

A

The Angular Router which represents a particular component view for a given URL is not part of Angular Core. It is available in library named @angular/router to import required router components. For example, we import them in app module as below,

25
Q

What is router state?

A

RouterState is a tree of activated routes. Every node in this tree knows about the “consumed” URL segments, the extracted parameters, and the resolved data. You can access the current RouterState from anywhere in the application using the Router service and the routerState property.

26
Q

What are router events?

A

During each navigation, the Router emits navigation events through the Router.events property allowing you to track the lifecycle of the route.

The sequence of router events is as below,

NavigationStart,
RouteConfigLoadStart,
RouteConfigLoadEnd,
RoutesRecognized,
GuardsCheckStart,
ChildActivationStart,
ActivationStart,
GuardsCheckEnd,
ResolveStart,
ResolveEnd,
ActivationEnd
ChildActivationEnd
NavigationEnd,
NavigationCancel,
NavigationError
Scroll

27
Q

What is activated route?

A

ActivatedRoute contains the information about a route associated with a component loaded in an outlet. It can also be used to traverse the router state tree. The ActivatedRoute will be injected as a router service to access the information. In the below example, you can access route path and parameters,

28
Q

What is the purpose of Wildcard route?

A

If the URL doesn’t match any predefined routes then it causes the router to throw an error and crash the app.

In this case, you can use wildcard route. A wildcard route has a path consisting of two asterisks to match every URL.

For example, you can define PageNotFoundComponent for wildcard route as below

29
Q

What are different types of compilation in Angular?

A

Angular offers two ways to compile your application,
Just-in-Time (JIT)
Ahead-of-Time (AOT)

30
Q

What is JIT?

A

Just-in-Time (JIT) is a type of compilation that compiles your app in the browser at runtime.

JIT compilation was the default until Angular 8, now default is AOT. When you run the ng build (build only) or ng serve (build and serve locally) CLI commands, the type of compilation (JIT or AOT) depends on the value of the aot property in your build configuration specified in angular.json. By default, aot is set to true.

31
Q

What is AOT?

A

Ahead-of-Time (AOT) is a type of compilation that compiles your app at build time. This is the default starting in Angular 9. When you run the ng build (build only) or ng serve (build and serve locally) CLI commands, the type of compilation (JIT or AOT) depends on the value of the aot property in your build configuration specified in angular.json. By default, aot is set to true.

32
Q

Why do we need compilation process?

A

The Angular components and templates cannot be understood by the browser directly. Due to that Angular applications require a compilation process before they can run in a browser. For example, In AOT compilation, both Angular HTML and TypeScript code converted into efficient JavaScript code during the build phase before browser runs it.

33
Q

What are the advantages with AOT?

A

Below are the list of AOT benefits,

Faster rendering: The browser downloads a pre-compiled version of the application. So it can render the application immediately without compiling the app.

Fewer asynchronous requests: It inlines external HTML templates and CSS style sheets within the application javascript which eliminates separate ajax requests.

Smaller Angular framework download size: Doesn’t require downloading the Angular compiler. Hence it dramatically reduces the application payload.

Detect template errors earlier: Detects and reports template binding errors during the build step itself

Better security: It compiles HTML templates and components into JavaScript. So there won’t be any injection attacks.

34
Q

What are the ways to control AOT compilation?

A

You can control your app compilation in two ways,
-> By providing template compiler options in the tsconfig.json file
-> By configuring Angular metadata with decorators

35
Q

What are the three phases of AOT?

A

The AOT compiler works in three phases,

Code Analysis: The compiler records a representation of the source

Code generation: It handles the interpretation as well as places restrictions on what it interprets.

Validation: In this phase, the Angular template compiler uses the TypeScript compiler to validate the binding expressions in templates.

36
Q

What is the purpose of any type cast function?

A

You can disable binding expression type checking using $any() type cast function(by surrounding the expression). In the following example, the error Property contacts does not exist is suppressed by casting user to the any type.

37
Q

What is Non null type assertion operator?

A

You can use the non-null type assertion operator to suppress the Object is possibly ‘undefined’ error. In the following example, the user and contact properties are always set together, implying that contact is always non-null if user is non-null.

The error is suppressed in the example by using contact!.email.

it is developer saying trust me, it wont be null

38
Q

What is type narrowing?

A

The expression used in an ngIf directive is used to narrow type unions in the Angular template compiler similar to if expression in typescript.

EXPRESSION USED IN NGIF TO NARROW TYPE

So *ngIf allows the typeScript compiler to infer that the data used in the binding expression will never be undefined.

39
Q

How do you describe various dependencies in angular application?

A

The dependencies section of package.json with in an angular application can be divided as follow,

  1. Angular packages: Angular core and optional modules; their package names begin @angular/.
  2. Support packages: Third-party libraries that must be present for Angular apps to run.
  3. Polyfill packages: Polyfills plug gaps in a browser’s JavaScript implementation.
40
Q

What is the purpose of common module?

A

The commonly-needed services, pipes, and directives provided by @angular/common module. Apart from these HttpClientModule is available under @angular/common/http.

41
Q

What is a service worker and its role in Angular?

A

A service worker is a script that runs in the web browser and manages caching for an application.

Starting from 5.0.0 version, Angular ships with a service worker implementation. Angular service worker is designed to optimize the end user experience of using an application over a slow or unreliable network connection, while also minimizing the risks of serving outdated content.

42
Q

What is Angular Ivy?

A

Angular Ivy is a new rendering engine for Angular. You can choose to opt in a preview version of Ivy from Angular version 8.

43
Q

Explain the features provided by Angular Language Service?

A

Autocompletion: Autocompletion can speed up your development time by providing you with contextual possibilities and hints as you type with in an interpolation and elements.

Error checking: It can also warn you of mistakes in your code.

Navigation: Navigation allows you to hover a component, directive, module and then click and press F12 to go directly to its definition.

44
Q

What is a builder?

A

A builder function is a function that uses the Architect API to perform a complex process such as “build” or “test”. The builder code is defined in an npm package. For example, BrowserBuilder runs a webpack build for a browser target and KarmaBuilder starts the Karma server and runs a webpack build for unit tests.

45
Q

What are the class decorators in Angular?

A

A class decorator is a decorator that appears immediately before a class definition, which declares the class to be of the given type, and provides metadata suitable to the type

The following list of decorators comes under class decorators,

@Component()
@Directive()
@Pipe()
@Injectable()
@NgModule()

46
Q

What are class field decorators?

A

The class field decorators are the statements declared immediately before a field in a class definition that defines the type of that field. Some of the examples are: @input and @output,

47
Q

What is declarable in Angular?

A

Declarable is a class type that you can add to the declarations list of an NgModule. The class types such as components, directives, and pipes comes can be declared in the module. The structure of declarations would be,

48
Q

What is a DI token?

A

A DI token is a lookup token associated with a dependency provider in dependency injection system. The injector maintains an internal token-provider map that it references when asked for a dependency and the DI token is the key to the map. Let’s take example of DI Token usage,