AngularJS Interview Questions and Answers
Explain component life cycle in Angular?
In Angular component life cycle in Angular goes through following stages.
Create and render children
Check for bound data changes and re-render
List the types of Data Binding supported by Angular5?
Answer : Angular 5 supports four types of Data Binding They are
What are the new features of Angular 2?
Answer : Angular 2 is a platform that encompasses a wide range of capabilities. Some new features were added in Angular 2 which includes:
- Universal server rendering- It is the library which is used to make building universal apps a smooth experience. It is an important feature of Angular 2.
- A mobile toolkit- It provides all the mobile toolkit and techniques to build high-performance mobile applications. The web applications which are developed using the mobile toolkit can be loaded on any device with or without internet connection which is a great advantage.
A command line interface-it can generate components, routes, services, and pipes with the help of commands.
How do you define the transition between two states in angular?
Answer : Transitions between two states take place so that we can build simple animations between two states driven by a model attribute. Transition basically means navigating from the current state to a new state. In angular, transition is an animation-specific function which is used in angular’s animation DSL language. Transition declares the sequence of animation steps that will be executed when entered value is satisfied. A function is provided an argument for a transition and it will be executed each time a state change occurs. In this, if the function is true, then the animation will run else it won’t get executed.
What is the difference between observable and promises?
Answer : The differences between observable and promises are:-
Observable is a more powerful way of handling HTTP asynchronous requests. Whereas, A promise handles a single event when an asynchronous operation completes or fails.
An observable is like a stream which allows passing zero or more events where the callback is called for each event. Whereas, A promise eventually calls the success or failed callback even when you don’t need the notification or the result it provides anymore.
Observable works with multiple values for a particular time. Whereas, Promises works with and even returns a single value at a time.
Observables can be canceled. Whereas, Promises cannot be canceled.
Observable supports map, filter, reduce and similar operators. Whereas, Promises have more readable codes with try/catch and async/await.
In observable, one operator ‘retry’ can be used to retry whenever needed. Whereas, Promises cannot be retried. A promise should have access to the original function that returned the promise in order to have a retry capability.
What is ECMAScript?
List the key components of Angular 2?
Answer : The Angular 2 comprises of the following key components:
Module – This is used to break the application into the logical pieces of the program code and each piece of code or module is designed to perform a single and unique task.
Component – This is used to bring the modules together.
Templates – This is used to define the Views of an Angular JS application.
Metadata – This is used to add more data to an Angular JS application.
Service – This component is used to develop the components, which can be used to share in the entire application.
Explain the component directory structure of angular4?
Answer :Here are the elements which are present in the component directory structure anf modules:-
module.ts- in this, the angular module is declared. @NgModule decorator is used which initializes the different aspects of angular applications. AppComponent is also declared in it.
components.ts- it simply defines the components in angular and this is the place where the app-root sector is also defined. A title attribute is also declared in the component.
component.html- it is the template file of the application which represents the visual parts of our components.
Explain property binding or one way binding in Angular js?
Answer : Basically, property binding means passing data from the component class and setting the value of a given element in the view. It is a one-way binding in which the data is passed from component to the class. It allows us to control the element property values from component to class. Property binding in angular can take place by three ways:
Interpolation can be used to define a value, as long as the value being defined is a string.
Wrapping brackets around the element property and binding it to the component property is the most common type of property binding.
The third way is by adding “bind” before the element property.
What’s new in Angular 5?
Answer : Angular 5 supports Typescript version 2.4
Angular 5 supports RxJS 5.5 which has new features like Pipeable Operators
A build tool to make the js bundles (files) lighter
Ahead of Time (AOT) is updated to be on by default
Events like ActivationStart and ActivationEnd are introduced in Router
Explain the concept of scope hierarchy.?
Answer : Each AngularJS application has only one root scope. Child controllers, however, create a scope for each view. When the new scopes are created, they are added to their parent root scope as child scopes. This creates a hierarchical structure when they are attached.
Explain what a digest cycle is in AngularJS?
Answer : During every digest cycle, all new scope model values are compared against the previous values. This is called dirty checking. If change is detected, watches set on the new model are fired and another digest cycle executes. This goes on until all models are stable.
Briefly describe how Angular digest cycle work?
Answer : The “digest cycle also known as “dirty checking” is a recursive loop that happens every time you update a model, if it takes more than 10 loops to resolve all bindings then it will fail. Why it needs to do this a few times is because a change in one part can cause a domino affect up the chain where there can be more changes. After every digest cycle, the DOM is then reflected with the changes in the model.
Explain what is directive and Mention what are the different types of Directive?
Answer :During the compilation process when specific HTML constructs are encountered a behavior or function is triggered, this function is referred to as directive. It is executed when the compiler encounters it in the DOM.
Different types of directives are
CSS class directives