AngularJS Interview Questions – Set 03

What is the module in AngularJS?

A module is a container for the different parts of the application like a controller, services, filters, directives, etc. It is treated as a main() method. All the dependencies of applications are generally defined in modules only. A module is created using an angular object’s module() method. For example:

var app = angular.module(‘myApp’, []);

What is AngularJS?

AngularJS is an open-source JavaScript framework used to build rich and extensible web applications. It is developed by Google and follows the MVC (Model View Controller) pattern. It supports HTML as the template language and enables the developers to create extended HTML tags which will help to represent the application’s content more clearly. It is easy to update and receive information from an HTML document. It also helps in writing a proper maintainable architecture which can be tested at a client-side.

What do you understand by validation of data in AngularJS?

AngularJS enriches form filling and validation. AngularJS provides client-side form validation. It checks the state of the form and input fields (input, text-area, select), and notify the user about the current state. It also holds the information about whether the input fields have been touched, or modified, or not.

There are following directives that can be used to track error:

  • $dirty
    It states that the value has been changed.
  • $invalid
    It states that the value which is entered is invalid.
  • $error
    It states the exact error.
    Moreover, we can use novalidate with a form declaration to disable the browser’s native form validation.

What do you understand by $watch?

In angularJS, $watch() function is used to watch the changes of variable in $scope object. Generally, the $watch() function is created internally to handle variable changes in the application.

If there is a need to create custom watch for some specific action then it’s better to use $scope.watch function. The $scope.watch() function is used to create a watch of some variable. When we register a watch, we pass two functions as parameters to the $watch() function:

  • A value function
  • A listener function
    An example is given below:

$scope.$watch(function() {},
function() {}
);
Here, the first function is the value function and the second function is the listener function.

Can we set an Angular variable from PHP session variable without sending an HTTP request?

Yes, we can perform it by injecting PHP in the required place. i.e.,

$scope.name=”;
It will work only if we use PHP to render the HTML and the above JavaScript in tag inside the PHP file.

How can someone set, get, and clear cookies in AngularJS?

AngularJS has a module known as ngCookies. Before we inject ngCookies, we should include angular-cookies.js into the application.

  • Set Cookies
    We can use the put method to set cookies in a key-value format.
    $cookies.put(“username”, $scope.username);
  • Get Cookies
    We can use the get method to get cookies.
    $cookies.get(‘username’);
  • Clear Cookies
    We can use the remove method to remove or clear cookies.
    $cookies.remove(‘username’)

What is the syntax for creating a new date object?

The syntax for creating new date object is given below:

$scope.newDate=new Date();

What are the features of AngularJS?

Some important features of AngularJS are given below:

  • MVC- In AngularJS, you just have to split your application code into MVC components, i.e., Model, View, and the Controller.
  • Validation- It performs client-side form validation.
  • Module- It defines an application.
  • Directive- It specifies behavior on the DOM element.
  • Template- It renders the dynamic view.
  • Scope- It joins the controller with the views.
  • Expression- It binds application data to HTML.
  • Data Binding- It creates a two-way data-binding between the selected element and the $ctrl.orderProp model.
  • Filter- It provides the filter to format data.
  • Service- It stores and shares data across the application.
  • Routing- It is used to build a single page application.
  • Dependency Injection- It specifies a design pattern in which components are given their dependencies instead of hard-coding them within the component.
  • Testing- It is easy to test any of the AngularJS components through unit testing and end-to-end testing.

What is routing in AngularJS?

Routing is one of the main features of the AngularJS framework, which is useful for creating a single page application (also referred to as SPA) with multiple views. It routes the application to different pages without reloading the application. In Angular, the ngRoute module is used to implement Routing. The ngView, $routeProvider, $route, and $routeParams are the different components of the ngRoute module, which help for configuring and mapping URL to views.

What are the main advantages of AngularJS?

Some of the main advantages of AngularJS are given below:

  • Allows us to create a single page application.
  • Follows MVC design pattern.
  • Predefined form validations.
  • Supports animations.
  • Open-source.
  • Cross-browser compliant.
  • Supports two-way data binding.
  • Its code is unit testable.