Angular js Interview question

What is AngularJS

“AngularJS is a JavaScript framework which simplifies binding JavaScript objects with HTML page elements.”

What is Directives in Angular?

 Directives are attributes decorated on the HTML elements. All directives start with the word “ng”. As the name says directive it directs Angular what to do.

For example below is a simple “ng-model” directive which tells angular that the HTML textbox “txt Name” has to be binded with the “ Name” property.

<input type=text id="txtName"  ng-model=" Name"/>.
 

What are controllers and need of ng-controller and ng-model in Angular?

“Controllers” are simple javascript function which provides data and logic to HTML page  As the name says controller they control how data flows from the server to HTML page.

What are expressions in Angular?

Angular expressions are unit of code which resolves to value. This code is written inside curly braces “{“.
Below are some examples of angular expressions:-
The below expression adds two constant values.

{{1+2}}

How can we initialize Angular application data?

We can use “ng-init” directive to achieve the same. You can see in the below example we have used “ng-init” directive to initialize the “pi” value.

Exp:
<body ng-app="myApp" ng-init="pi=3.14">
The value of pi is {{pi}}
</body>
  

Explain $scope in Angular?

“$scope” is an object instance of a controller. “$scope” object instance
 get’s created when “ng-controller” directive is encountered.

Scope object can be used to pass the data from model to HTML page using controller function, any data which we need to provide the data we can use $scope object.  

 

What is “$rootScope” and how is it related with “$scope”?

“$rootScope” is a parent object of all “$scope” angular objects created in a web page.

 

How is the data binding in Angular ?

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

How do we make HTTP get and post calls in Angular?

To make HTTP calls we need to use the “$http” service of Angular. In order to use the http services you need to make provide the “$http” as a input in your function parameters as shown in the below code.
 exp;
function empController($scope,$http)
{
 $scope.Add = function()
 {
            $http({ method: "GET", url: "http://localhost:8438/SomeMethod" 
                 }).success(function (data, status, headers, config)
  {
                   // Here goes code after success
  }
 }
}
 
 
“$http” service API needs at least three things:-
  • First what is the kind of call “POST” or “GET”.
  • Second the resource URL on which the action should happen.
  • Third we need to define the “success” function which will be executed once we get the response from the server.
$http({ method: "GET", url: "http://localhost:8438/SomeMethod" })
     .success(function (data, status, headers, config)
            {
            // Here goes code after success
          } 

 

No comments:

Post a Comment