Here are 100 AngularJS interview questions along with their short answers:
Basics:
- What is AngularJS?
- AngularJS is a JavaScript framework developed by Google for building dynamic web applications.
- What are the key features of AngularJS?
- Key features include two-way data binding, dependency injection, directives, and a modular structure.
- Explain two-way data binding in AngularJS.
- Two-way data binding allows automatic synchronization of data between the model and the view, ensuring that changes in one are reflected in the other.
- What is dependency injection in AngularJS?
- Dependency injection is a design pattern that allows you to inject dependencies (services or objects) into components.
- What is an AngularJS directive?
- A directive is a marker in the DOM that tells AngularJS to attach a specific behavior to an HTML element.
- What is the role of controllers in AngularJS?
- Controllers are responsible for managing the data and behavior of a view in AngularJS.
- What is the AngularJS module?
- A module is a container for different parts of an AngularJS application, such as controllers, services, and directives.
- Explain the AngularJS digest cycle.
- The digest cycle is the process by which AngularJS keeps data synchronized between the model and the view.
- What is AngularJS’s ng-app directive used for?
- The
ng-app
directive defines the root element of an AngularJS application and initializes it.
- What is a template in AngularJS?
- A template is an HTML file or string that defines the structure of a view in an AngularJS application.
Directives:
- What is the ng-model directive used for?
- The
ng-model
directive is used for two-way data binding, linking an input element’s value to a model property.
- The
- Explain the ng-repeat directive in AngularJS.
- The
ng-repeat
directive is used for iterating over a collection and rendering elements for each item.
- The
- What is the purpose of the ng-if directive?
- The
ng-if
directive conditionally renders or removes an element from the DOM based on an expression.
- The
- What is the ng-show and ng-hide directive used for?
ng-show
andng-hide
directives conditionally display or hide elements based on an expression.
- Explain the ng-click directive.
- The
ng-click
directive is used to define a function to be executed when an element is clicked.
- The
- What is the ng-src directive used for?
- The
ng-src
directive binds thesrc
attribute of an HTML element to an expression, typically used for images.
- The
- What is the purpose of the ng-class directive in AngularJS?
- The
ng-class
directive dynamically applies CSS classes to elements based on expression evaluation.
- The
- What is the ng-style directive used for?
- The
ng-style
directive allows you to dynamically set CSS styles on an element based on an expression.
- The
- Explain the ng-switch directive in AngularJS.
- The
ng-switch
directive is used for conditionally rendering content based on a provided value.
- The
- What is the ng-include directive used for?
- The
ng-include
directive is used to include an external HTML file into the current template.
- The
Controllers:
- How do you define a controller in AngularJS?
- A controller is defined using the
controller
method and attached to a module.
- A controller is defined using the
- What is the purpose of the $scope object in AngularJS controllers?
- The
$scope
object is a bridge between the controller and the view, allowing data and functions to be accessible in the template.
- The
- How do you share data between controllers in AngularJS?
- You can share data between controllers using services or the
$rootScope
object.
- You can share data between controllers using services or the
- What is a controller alias in AngularJS?
- A controller alias is used to avoid naming conflicts when multiple controllers are used within the same scope.
- Explain the concept of controller inheritance in AngularJS.
- Child controllers inherit properties and methods from their parent controllers, allowing for a hierarchy of controllers.
- What is a controller constructor function in AngularJS?
- A controller constructor function is used to define a controller and is invoked when the controller is instantiated.
- What is the difference between the $scope object and the this keyword in a controller?
$scope
is used for data binding between the controller and the view, whilethis
is used for defining functions and properties directly on the controller.
- How do you handle controller initialization in AngularJS?
- Controller initialization can be handled by using the
controllerAs
syntax or the
ng-controller
directive. - Controller initialization can be handled by using the
- What is the role of the $inject property in AngularJS controllers?
- The
$inject
property is used to declare and specify dependencies for a controller when minification renames function parameters.
- The
- What is a route parameter in AngularJS?
- Route parameters allow dynamic values to be passed in the URL and accessed by the controller.
Services and Factories:
- What is an AngularJS service?
- A service is a singleton object that provides methods and data that can be shared across multiple parts of an application.
- How do you create a custom service in AngularJS?
- A custom service is created using the
service
orfactory
method and registered with a module.
- A custom service is created using the
- What is the difference between an AngularJS service and factory?
- A service is a constructor function, while a factory is a function that returns an object or value.
- What is the purpose of the $http service in AngularJS?
- The
$http
service is used for making HTTP requests and interacting with RESTful APIs.
- The
- Explain the $resource service in AngularJS.
$resource
is a higher-level service for working with RESTful APIs and simplifying CRUD operations.
- What is the purpose of the $q service in AngularJS?
- The
$q
service provides a promise-based interface for handling asynchronous operations.
- The
- What is the AngularJS $location service used for?
- The
$location
service allows you to work with the browser’s URL, navigate to different routes, and access URL parameters.
- The
- What is the $rootScope object in AngularJS?
- The
$rootScope
object is the parent scope for all scopes in an AngularJS application and can be used to share data across controllers.
- The
- Explain the $timeout and $interval services in AngularJS.
$timeout
is used to delay the execution of a function, and$interval
is used for repeatedly running a function at specified intervals.
- What is a provider in AngularJS, and how is it used to configure services?
- A provider is used to configure services before they are injected into controllers, directives, or other services.
Dependency Injection:
- What is dependency injection (DI) in AngularJS?
- Dependency injection is a design pattern that enables the injection of dependencies into a component rather than hard-coding them.
- How does AngularJS implement dependency injection?
- AngularJS uses the
$injector
to manage and inject dependencies into controllers, services, and other components.
- AngularJS uses the
- Explain the concept of implicit and explicit annotation in AngularJS dependency injection.
- Implicit annotation relies on the order of parameters to match dependencies, while explicit annotation uses an array to specify dependencies.
- What is a service dependency in AngularJS?
- A service dependency is a dependency on a reusable service that can be injected into controllers, directives, or other services.
- What are constant dependencies in AngularJS?
- Constant dependencies are values that can be injected into components and are typically used for configuration settings.
- How do you inject a service into a controller in AngularJS?
- You can inject a service into a controller by specifying it as a parameter in the controller function.
- What is the role of the
$inject
property in AngularJS dependency injection?- The
$inject
property is used to explicitly specify the dependencies for a component, which can be helpful when minification is applied.
- The
- Explain the concept of circular dependency in AngularJS.
- Circular dependencies occur when two or more services depend on each other, which can lead to runtime errors.
- How can you resolve circular dependencies in AngularJS?
- Circular dependencies can be resolved by using
$injector.get()
to defer the injection of a service until it is needed.
- Circular dependencies can be resolved by using
- What is the purpose of the
$provide
service in AngularJS?- The
$provide
service is used to configure and define services, factories, and providers.
- The
Routing:
- What is routing in AngularJS?
- Routing is a mechanism in AngularJS for defining navigation and views based on URLs.
- How do you configure routing in AngularJS?
- Routing is configured using the
$routeProvider
or the$stateProvider
service to define routes, templates, and controllers.
- Routing is configured using the
- What is a route in AngularJS?
- A route is a URL path mapped to a specific template and controller.
- What is a route parameter in AngularJS?
- Route parameters are placeholders in URLs that can capture dynamic values and pass them to a controller.
- Explain the concept of nested routes in AngularJS.
- Nested routes allow you to define child routes within a parent route, creating a hierarchical navigation structure.
- How do you set up and navigate to routes in AngularJS?
- Routes are set up in the configuration phase using
$routeProvider
or$stateProvider
, and navigation is achieved using links or programmatically with$location
.
- Routes are set up in the configuration phase using
- What is route resolve in AngularJS, and how does it work?
- Route resolve is a way to ensure that certain data is available before the route is activated and the associated controller is instantiated.
- How do you pass data between routes in AngularJS?
- Data can be passed between routes using route parameters, custom services, or the
$rootScope
object.
- Data can be passed between routes using route parameters, custom services, or the
- What is route inheritance in AngularJS?
- Route inheritance allows child routes to inherit properties and resolve data from their parent route.
- What is HTML5 mode in AngularJS routing?
- HTML5 mode allows for cleaner URLs without the hash (#) symbol, improving SEO and user experience.
Custom Directives:
- What is a custom directive in AngularJS?
- A custom directive is a reusable component or behavior added to an HTML element.
- How do you create a custom directive in AngularJS?
- A custom directive is created using the
directive
method and defining a directive factory function.
- A custom directive is created using the
- Explain the options object when creating a custom directive in AngularJS.
- The options object includes properties like
restrict
,template
,controller
, andlink
for configuring the directive’s behavior and appearance.
- The options object includes properties like
- What is the
restrict
property in a custom directive in AngularJS?- The
restrict
property specifies how the directive can be used, such asE
(element),A
(attribute), orC
(class).
- The
- What is the
link
function in a custom directive in AngularJS?- The
link
function is responsible for registering DOM listeners and updating the DOM based on the directive’s behavior.
- The
- How do you isolate the scope of a custom directive in AngularJS?
- You can isolate the scope of a directive by setting the
scope
property to an object in the directive definition.
- You can isolate the scope of a directive by setting the
- Explain the difference between
@
,=
, and&
bindings in a custom directive’s scope.@
is used for
=
is used for two-way binding, and&
is used to pass a function to the directive. - What is the
transclude
property in a custom directive in AngularJS?transclude
allows you to insert content into the directive’s template and is often used with theng-transclude
directive.
- What is the
compile
function in a custom directive in AngularJS?- The
compile
function is used to modify the DOM or the directive before it’s linked to the scope and controller.
- The
- How do you use the
require
property in a custom directive in AngularJS?- The
require
property specifies which controllers to require from other directives on the same element.
- The
Filters:
- What is a filter in AngularJS?
- A filter is a function that formats, transforms, or filters data in an AngularJS expression.
- How do you use built-in filters in AngularJS?
- Built-in filters are used in expressions with the
|
symbol, such as{{ data | filterName }}
.
- Built-in filters are used in expressions with the
- Explain the
filter
filter in AngularJS.- The
filter
filter is used to filter an array based on a specified criteria.
- The
- What is the
orderBy
filter used for in AngularJS?- The
orderBy
filter sorts an array based on a specified property and order.
- The
- What is the
currency
filter in AngularJS?- The
currency
filter formats a number as currency using a specified currency symbol.
- The
- Explain the
date
filter in AngularJS.- The
date
filter formats a date object as a string with a specified format.
- The
- What is the
uppercase
andlowercase
filter used for in AngularJS?- The
uppercase
filter converts a string to uppercase, while thelowercase
filter converts it to lowercase.
- The
- How do you create a custom filter in AngularJS?
- A custom filter is created using the
filter
method and returning a function that defines the filter’s behavior.
- A custom filter is created using the
- What is the
json
filter used for in AngularJS?- The
json
filter formats an object as a JSON string.
- The
- What is the
limitTo
filter in AngularJS?- The
limitTo
filter limits an array or string to a specified number of elements or characters.
- The
Testing:
- What is unit testing in AngularJS?
- Unit testing in AngularJS involves testing individual components, such as controllers, services, and directives, in isolation.
- How do you set up unit tests in AngularJS?
- Unit tests are typically set up using testing frameworks like Jasmine and testing utilities like Karma.
- What is end-to-end (E2E) testing in AngularJS?
- E2E testing in AngularJS involves testing the entire application from the user’s perspective, simulating real user interactions.
- How do you set up E2E tests in AngularJS?
- E2E tests can be set up using tools like Protractor, which automates user interactions in a real browser.
- What is dependency injection in the context of testing AngularJS components?
- Dependency injection in testing allows you to provide mock or stub dependencies for a component, isolating it from external services.
- Explain the role of the
beforeEach
andit
functions in Jasmine testing in AngularJS.beforeEach
is used to set up common test conditions, andit
defines individual test cases.
- How do you mock dependencies in AngularJS unit tests?
- Dependencies can be mocked using Jasmine’s
spy
functions or by providing custom implementations.
- Dependencies can be mocked using Jasmine’s
- What is test-driven development (TDD) in AngularJS?
- TDD is a development approach in which tests are written before code, ensuring that the code meets the expected requirements.
- What is the role of assertions in unit testing in AngularJS?
- Assertions are used to verify that the actual behavior of a component matches the expected behavior defined in the test.
- How do you perform asynchronous testing in AngularJS unit tests?
- Asynchronous tests can be handled using Jasmine’s
done
function, promises, or$timeout
to wait for async operations.
- Asynchronous tests can be handled using Jasmine’s
AngularJS 1.x vs. Angular (Angular 2+):
- What are the key differences between AngularJS 1.x and Angular (Angular 2+)?
- Major differences include the use of TypeScript, a component-based architecture, and the absence of controllers in Angular.
- How does Angular handle two-way data binding differently from AngularJS?
- Angular uses property binding and event binding to achieve two-way data binding, while AngularJS uses
ng-model
.
- Angular uses property binding and event binding to achieve two-way data binding, while AngularJS uses
- What is the difference in the component structure between AngularJS and Angular?
- Angular is based on components, while AngularJS uses controllers and directives.
- How does Angular handle routing compared to AngularJS?
- Angular uses the Angular Router module, which is more powerful and configurable than AngularJS’s
$routeProvider
.
- Angular uses the Angular Router module, which is more powerful and configurable than AngularJS’s
- What is the difference in dependency injection between AngularJS and Angular?
- Angular’s dependency injection is more hierarchical and flexible, while AngularJS relies on
$injector
for injection.
- Angular’s dependency injection is more hierarchical and flexible, while AngularJS relies on
- How does Angular 2+ support static type checking compared to AngularJS?
- Angular 2+ uses TypeScript for static type checking, providing better tooling and compile-time error checking.
- What is the difference in the change detection mechanism between AngularJS and Angular?
- Angular uses a zone.js-based change detection mechanism, which is more efficient than AngularJS’s digest cycle.
- How does Angular handle forms and validation differently from AngularJS?
- Angular uses a reactive approach with the
FormsModule
for handling forms and validation, while AngularJS uses theng-form
directive and$valid
properties.
- Angular uses a reactive approach with the
- What are some migration strategies for moving from AngularJS to Angular (Angular 2+)?
- Strategies include incremental migration, running AngularJS and Angular side by side, and upgrading individual components.
- How does Angular’s development environment and tooling differ from AngularJS?
- Angular offers improved tooling, such as the Angular CLI, for generating and managing projects, making it more developer-friendly.