Angularjs Interview Questions and Answers Set 9

81.The following HTML is given button ALERT How can the attached controller look like, to show an alert window when clicking on the button?

AngularJS Events are the functionalities that allow web applications to interact with user inputs like mouse click, keyboard inputs, mouse hover, etc. Events need to be handled in web-based applications in order to perform specific tasks and actions. It is triggered when a specific action is performed by the user.

82.List out the differences between config and run methods in AngularJS?

Config Block – Constants and providers can be injected into config blocks and this block can be created using config method.

Run Block – Run block will be executed after config block. Run block is used to inject constants and instances and this block can be created using run method.

Eg :

angular.module(‘myTestModule’, []).
config(function (injectables) {
// config block
run(function (injectables) {
// run block

83.How Would You Use An Angular Service To Pass Data Between Controllers? Explain With Examples?

Using a service is the best practice in Angular to share data between controllers. Here is a step by step example to demonstrate data transfer.

We can prepare the data service provider in the following manner.

app.service(‘dataService’, function() {
var dataSet = [];
var addData = function(newData) {
var getData = function(){
return dataSet;
return {    addData: addData,    getData: getData  };

Now, we’ll inject the service dependency into the controllers.

Say, we have two controllers – pushController and popController.

The first one will add data by using the data service provider’s addData method. And the latter will fetch this data using the service provider’s getData method.

app.controller(‘pushController’, function($scope, dataService) {
$scope.callToAddToProductList = function(currObj){        dataService.addData(currObj);

app.controller(‘popController’, function($scope, dataService) {
$scope.dataSet = dataService.getData();

84.What is the difference between “.$digest()” and “.$apply()”? Why would you ever call “.$digest()” on a scope?

$digest() gets called without any arguments. $apply() takes a function that it will execute before doing any updates. The other difference is what they affect. $digest() will update the current scope and any child scopes.

Weekend / Weekday Batch

86. What is the purpose of base href tag?

The href attribute is used to specify the base URL for all relative URLs on a page.During navigation, the base href tag is used by the Angular router as a base path to the component, template, and module files.

87. What is hoisting in JS?

JavaScript Hoisting refers to the process whereby the compiler allocates memory for variable and function declarations prior to execution of the code. Conceptually hoisting is often presented as the compiler “splitting variable declaration and initialization, and moving (just) the declarations to the top of the code.

88.How did you get the value from input box using JQuery?

To get the textbox value, use the jQuery val() function. For example, $(‘input:textbox’). val() – Get textbox value.

89.. ln component and service which logic that we have to write?

The component controller logic should contain logic to subscribe to service events, or call service methods for the purposes of retrieving the model and presenting it to the view.

90.What is DOM?

The full form of DOM is Document Object Model, and it is responsible for representing the content of a web page and changes in the architecture of an application. Here, all the objects are organized in the form of a tree, and the document can easily be modified, manipulated, and accessed only with the help of APIs.