Angularjs Interview Questions and Answers Set 5

41.What is $rootscope in AngularJS?

The $rootScope is the top-most scope. An app can have only one $rootScope which will be shared among all the components of an app. Hence it acts as a global variable. All other $scopes are children of the $rootScope. Since $rootScope is a global, which means that anything you add here, automatically becomes available in $scope in all controller. To add something in $rootScope, you need to use function which ensures that it will run prior to the rest of the app. You may say that “run” function is like “main” method of angular app.

42.What is the difference between Rootscope and Scope in AngularJS?

Rootscope – Rootscope is the top most scope and one application can have an only one rootscope and will be shared among all the components.

Scope – Scope will act as a glue between view and controller and scopes are arranged in the hierarchical structure.

43.Give an example for Rootscope in AngularJS?

Rootscope acts as a global variable and below is the sample code for the same –

<body ng-app=”mytestApp”>
<div ng-controller=”MyFirstController” style=”border:2px solid blue; padding:5px”>
Hi {{mymsg}}!
<br />
Hi {{myname}}! (rootScope)
<br />
<div ng-controller=”MySecondController” style=”border:2px solid green; padding:5px”>
Hello {{mymsg}}!
<br />
Hi {{name}}! (rootScope)

<script src=”/lib/angular.js”></script>
var app = angular.module(‘mytestApp’, []);
app.controller(‘MyFirstController’, function ($scope, $rootScope) {
$scope.msg = ‘Good Morning’;
$ = ‘Test My scope’;

app.controller(‘MySecondController’, function ($scope, $rootScope) {
$scope.msg = ‘Good Night’;
$scope.CheckName = $;

44.Explain what is Angular Expression? Explain what is key difference between angular expressions and JavaScript expressions?

Like JavaScript, Angular expressions are code snippets that are usually placed in binding such as {{ expression }}

The key difference between the JavaScript expressions and Angular expressions:-

Context : In Angular, the expressions are evaluated against a scope object, while the Javascript expressions are evaluated against the global window

Forgiving: In Angular expression evaluation is forgiving to null and undefined, while in Javascript undefined properties generates TypeError or ReferenceError

No Control Flow Statements: Loops, conditionals or exceptions cannot be used in an angular expression

Filters: To format data before displaying it you can use filters

45.What are expressions in AngularJS?

Expressions in AngularJS are just like JavaScript code snippets. JavaScript code is usually written inside double braces: {{expression}}. In other words, Angular Expressions are JavaScript code snippets with limited sub-set. Expressions are included in the HTML elements.

Like JavaScript expressions, AngularJS expressions can also have various valid expressions. We can use the operators between numbers and strings, literals, objects and arrarys inside the expression {{ }}. For example,

{{ 2 + 2 }} (numbers)
{{Name + ” ” + email}} (string)
{{ Country.Name }} (object)
{{ fact[4] }} (array)

Weekend / Weekday Batch

46.Explain One-way binding in AngularJS?

This type of binding is being introduced in angularJS version – 1.3 and double colon – “::” is used for one-time expression. Below is the example of using it –

Eg : {{:: mytestname }}

47.What Is The Difference Between One-Way Binding And Two-Way Binding?

The main difference between one-way binding and two-way binding is as follows.

In one-way binding, the scope variable in the HTML gets initialized with the first value its model specifies.

In two-way binding, the scope variable will change its value whenever the model gets a different value

48.Give an example of Data binding in AngularJS?

<div ng-app=”” ng-init=”quantity=10;cost=5″ class=”ng-scope”>
<b>Total Cost: {{cost * quantity}}</b>

49.What are directives in AngularJS?

Directives are markers on a DOM element (such as an attribute, element name, comment or CSS class) that tell AngularJS to attach a specified behaviour to that DOM element or even transform the DOM element and its children. When AngularJS finds the directive at the time of rendering then it attaches the requested behaviour to the DOM element. Angular comes with a set of these directives built-in, like ngBind, ngModel, and ngClass.

50.Explain directive in AngularJS?

Directive are the combination of javascript code and AngularJS markup. Javascript directive code defines the behaviours and template data of HTML elements. There are some built-in directives like – “ng-controller”,”ng-app”, “ng-repeat” etc.