AngularJS Interview Questions
Category: AngularJS 2 Posted:Mar 02, 2017 By: admin1. Explain data binding in AngularJS.
According to AngularJS.org, “Data-binding in Angular apps is the automatic synchronization of data between the model and view components. The way that Angular implements data-binding lets you treat the model as the single-source-of-truth in your application. The view is a projection of the model at all times. When the model changes, the view reflects the change and vice versa.”
There are two ways of data binding:
- Data mining in classical template systems
- Data binding in angular templates
2. Name the key features of AngularJS?
The key features of AngularJS are:
- Scope
- Controller
- Model
- View
- Services
- Data Binding
- Directives
- Filters
- Testable
3. What are directives in AngularJS?
A core feature of AngularJS, directives are attributes that allow you to invent new HTML syntax, specific to your application. They are essentially functions that execute when the Angular compiler finds them in the DOM. Some of the most commonly used directives are ng-app,ng-controller and ng-repeat.
The different types of directives are:
- Element directives
- Attribute directives
- CSS class directives
- Comment directives
4. What are Controllers in AngularJS?
Controllers are Javascript functions which provide data and logic to HTML UI. As the name suggests, they control how data flows from the server to HTML UI.
5. What is Angular Expression? How do you differentiate between Angular expressions and JavaScript expressions?
Angular expressions are code snippets that are usually placed in binding such as {{ expression }} similar to JavaScript.
The main differences between Angular expressions and JavaScript expressions are:
- Context: The expressions are evaluated against a scope object in Angular, while Javascript expressions are evaluated against the global window
- Forgiving: In Angular expression, the evaluation is forgiving to null and undefined whereas in JavaScript undefined properties generate TypeError or ReferenceError
- No Control Flow Statements: We cannot use loops, conditionals or exceptions in an Angular expression
- Filters: In Angular, unlike JavaScript, we can use filters to format data before displaying it
6. What is the difference between link and compile in Angular.js?
- Compile function is used for template DOM Manipulation and to collect all the directives.
- Link function is used for registering DOM listeners as well as instance DOM manipulation and is executed once the template has been cloned.
7. What are the characteristics of ‘Scope’?
“Scope” is an object that refers to the application model. It is an execution context for expressions. Scopes are arranged in a hierarchical structure which mimic the DOM structure of the application. Scopes can watch expressions and propagate events. The characteristics of Scope are:
- Scopes provide APIs ($watch) to observe model mutations.
- Scopes provide APIs ($apply) to propagate any model changes through the system into the view from outside of the “Angular realm” (controllers, services, Angular event handlers).
- Scopes can be nested to limit access to the properties of application components while providing access to shared model properties. Nested scopes are either “child scopes” or “isolate scopes”. A “child scope” (prototypically) inherits properties from its parent scope. An “isolate scope” does not. See isolated scopes for more information.
- Scopes provide the context against which expressions are evaluated. For example,
{{username}}
expression is meaningless, unless it is evaluated against a specific scope which defines the username property.
8. What are the advantages of using the Angular.js framework?
Angular.js framework has the following advantages:
- Supports two way data-binding
- Supports MVC pattern
- Support static template and angular template
- Can add custom directive
- Supports REST full services
- Supports form validations
- Support both client and server communication
- Support dependency injection
- Applying Animations
- Event Handlers
9. What is the difference between AngularJS and backbone.js?
AngularJS combines the functionalities of most third party libraries and supports individual functionalities required to develop HTML5 Apps. While Backbone.js does these jobs individually.
10. Explain what is injector in AngularJS?
An injector is a service locator, used to retrieve object instance as defined by the provider, instantiate types, invoke methods, and load modules.
11. What is factory method in AngularJS?
A Factory method is used for creating a directive. It is invoked when the compiler matches the directive for the first time. We can invoke the factory method using $injector.invoke.
Syntax: module.factory( 'factoryName', function );
Result: When declaring factoryName as an injectable argument you will be provided with the value that is returned by invoking the function reference passed to module.factory.
12. What is ng-app, ng- init and ng-model?
- ng-app: Initializes application.
- ng-model: Binds HTML controls to application data.
- ng-Controller: Attaches a controller class to view.
- ng-repeat: Bind repeated data HTML elements. It is like a “for loop”.
- ng-if: Bind HTML elements with the condition.
- ng-show: Used to show the HTML elements.
- ng-hide: Used to hide the HTML elements.
- ng-class: Used to assign CSS class.
- ng-src: Used to pass the URL image etc.
13. Does Angular use the jQuery library?
Yes, Angular can use jQuery if it’s present in the app when the application is being bootstrapped. If jQuery is not present in the script path, Angular falls back to its own implementation of the subset of jQuery that we call jQLite.
14. Can AngularJS have multiple ng-app directives in a single page?
No. Only one AngularJS application can be auto-bootstrapped per HTML document. The first ngApp found in the document will be used to define the root element to auto-bootstrap as an application. If another ng-app directive has been placed then it will not be processed by AngularJS and we will need to manually bootstrap the second app, instead of using second ng-app directive.
15. Can angular applications (ng-app) be nested within each other?
No. AngularJS applications cannot be nested within each other.
16. What is internationalization and how to implement it in AngularJS?
Internationalization is a way in which you can show locale specific information on a website. AngularJS supports inbuilt internationalization for three types of filters:currency, date and numbers. To implement internalization, we only need to incorporate corresponding js according to locale of the country. By default, it handles the locale of the browser.
17. On which types of the component can we create a custom directive?
AngularJS provides support to create custom directives for the following:
- Element directives − Directive activates when a matching element is encountered.
- Attribute − Directive activates when a matching attribute is encountered.
- CSS − Directive activates when a matching css style is encountered.
- Comment − Directive activates when a matching comment is encountered.
18. What is $rootscope in AngularJS?
Every application has a single root scope. All other scopes are descendant scopes of the root scope. Scopes provide separation between the model and the view, via a mechanism for watching the model for changes. They also provide event emission/broadcast and subscription facility.
19. Can we have nested controllers in AngularJS?
Yes, we can create nested controllers in AngularJS. Nested controllers are defined in a hierarchical manner while using in View.
20. What is bootstrapping in AngularJS?
Bootstrapping in AngularJS is nothing but initializing, or starting the Angular app. AngularJS supports automatic and manual bootstrapping.
- Automatic Bootstrapping: this is done by adding the ng-app directive to the root of the application, typically on the tag or tag if you want angular to bootstrap your application automatically. When angularJS finds ng-app directive, it loads the module associated with it and then compiles the DOM.
- Manual Bootstrapping: Manual bootstrapping provides you more control on how and when to initialize your angular App. It is useful where you want to perform any other operation before Angular wakes up and compile the page.
21. What does SPA (Single Page Application) mean? How can we implement SPA with Angular?
Single Page Applications (SPAs) are web apps that load a single HTML page and dynamically update that page as the user interacts with the app. In an SPA the page never reloads, though parts of the page may refresh. This reduces the round trips to the server to a minimum.
It’s a concept where we create a single shell page or master page and load the web pages inside that master page instead of loading pages from the server by doing back posting. We can implement SPA with Angular using Angular routes. You can read up about SPAs here.
22. Why AngularJS?
AngularJS lets us extend HTML vocabulary for our application resulting in an expressive, readable, and quick to develop environment. Some of the advantages are:
- MVC implementation is done right.
- It extends HTML using directives, expression and data binding techniques to define a powerful HTML template.
- Two way data-binding, form validations, routing supports, inbuilt services.
- REST friendly.
- Dependency injection support.
- It helps you to structure and test your JavaScript code.
23. Is AngularJS compatible with all browsers?
Yes AngularJS is compatible with the following browsers: Safari, Chrome, Firefox, Opera 15, IE9 and mobile browsers (Android, Chrome Mobile, iOS Safari).
24. How to implement routing in AngularJS?
It is a five-step process:
Step 1: – Add the “Angular-route.js” file to your view.
Step 2: – Inject “ngroute” functionality while creating Angular app object.
Step 3: – Configure the route provider.
Step 4: – Define hyperlinks.
Step 5: – Define sections where to load the view.
25. Explain $q service, deferred and promises.
- ‘Promises’ are post processing logics which are executed after some operation/action is completed whereas ‘deferred’ is used to control how and when those promise logics will execute.
- We can think about promises as “WHAT” we want to fire after an operation is completed while deferred controls “WHEN” and “HOW” those promises will execute.
- “$q” is the angular service which provides promises and deferred functionality.