AngularJS Interview Question -2

What are expressions in AngularJS?

Angular expressions are unit of code which resolves to value. This code is written inside curly braces “{“.

AngularJS expressions are much like JavaScript expressions: They can contain literals, operators, and variables.

Example {{ 5 + 5 }} or {{ firstName + " " + lastName }}
<div ng-app="">
  <p>My first expression: {{ 5 + 5 }}</p>

The below expression displays a controller scoped variable.

<div ng-controller="CustomerVM">
The value of Customer code is {{CustomerCode}} 

Difference between AngularJS Expressions and JavaScript Expressions

Like JavaScript expressions, AngularJS expressions can contain literals, operators, and variables.

Unlike JavaScript expressions, AngularJS expressions can be written inside HTML.

AngularJS expressions do not support conditionals, loops, and exceptions, while JavaScript expressions do.

AngularJS expressions support filters, while JavaScript expressions do not.

What is the digest cycle, watchers and dirty checking?

Digest cycle: - It is a simple loop which updates the model and view.
Watchers :- They are listeners which are attached to expression and angular directives and fire when the model data changes.
Dirty check :- This is a extra digest loop which runs to check any cascading left over updates due to the first digest cycle.

Lets look in details.

Angular is a MVW framework. It helps us to bind the model and the view. 

In other words when any change happens in the model the view gets updated. 
This updation of the model and the view is done by a loop called as digest cycle.
Digest cycle follows four important steps:-

Step 1:- Some kind of event is triggered by the end user like typing (onchange), button click etc and due to this activity model value changes.

Step 2:- Angular first checks if the new value and old values are same. If they are same he does not do anything. If they are not it then it invokes the digest cycle.
Step 3:- Digest cycle then runs through the scope objects to check which objects are getting affected because of this change. Every object in the scope have watchers. Watchers as the name says it listens whether the model has changed or not. Digest cycle informs the watchers about the model change and then watchers synchronize the view with the model data.
Step 4 :- In step 3 watchers update the view and due that update its very much possible that the model changes again. Now due to this model change we have to reevaulate the view again. So the digest loop runs once again to ensure that all things are synched up. This second loop which runs is termed as dirty check loop.
Below is the figure where in we have highlighted all the four steps.

How do you reset a “$timeout”, and disable a “$watch()”?

The key to both is assigning the result of the function to a variable.

To cleanup the timeout, just “.cancel()” it:

var customTimeout = $timeout(function () {
  // arbitrary code
}, 55);

The same applies to “$interval()”.

To disable a watch, just call it.

// .$watch() returns a deregistration function that we store to a variable
var deregisterWatchFn = $rootScope.$watch(‘someGloballyAvailableProperty’, function (newVal) {
  if (newVal) {
    // we invoke that deregistration function, to disable the watch

List at least three ways to communicate between modules of your application using core AngularJS functionality.

Common ways to communicate between modules of your application using core AngularJS functionality include:

Using services

Using events
By assigning models on $rootScope
Directly between controllers, using $parent, $$childHead, $$nextSibling, etc.
Directly between controllers, using ControllerAs, or other forms of inheritance

Do I need Jquery for AngularJS?
No , you do not need Jquery for Angular. It’s independent of Jquery.

How is the data binding in AngularJS ?

Its two way binding. So whenever you make changes in one entity the other entity also gets updated.

Mention what are the styling form that ngModel adds to CSS classes ?

ngModel adds these CSS classes to allow styling of form as well as control

ng- valid
ng- invalid

Difference between angular-route and angular-ui-router
See below description

Explain AngularJS boot process.

When the page is loaded in the browser, following things happen:

• HTML document is loaded into the browser, and evaluated by the browser. AngularJS JavaScript file is loaded; the angular global object is created. Next, JavaScript which registers controller functions is executed.

• Next AngularJS scans through the HTML to look for AngularJS apps and views. Once view is located, it connects that view to the corresponding controller function.

• Next, AngularJS executes the controller functions. It then renders the views with data from the model populated by the controller. The page gets ready.

AngularJS Interview Question        


Post a Comment