Lets Have a fun with Technology.


Follow by Email

Thursday, May 26, 2016

AngularJS Interview Question -1

What is the Singl Page Application (SPA)
Single Page Application is one in which we have a shell page and we can load multiple views into that.
Traditional application, as you know you typically blink and load everything again. It’s not very efficient on the bandwidth, especially in the mobile world.

In a SPA we can load the initial content upfront and then the different views or the little kind of miniweb pages can be loaded on the fly and embedded into the shell.

What is AngularJS?

AngularJS is a javascript framework used for creating single web page applications. 
AngularJS is a structural framework for dynamic web apps. 
It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. 
Angular's data binding and dependency injection eliminate much of the code you would otherwise have to write.

Explain Directives in AngularJS

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 

“txtAgentName” has to be binded with the “AgentName” property.

<input type=text id="txtAgentName"  ng-model="AgentName"/>

Some of the most commonly used directives are ng-app,ng-controller and ng-repeat.

List out Main Features of AngularJS.

  1.   Data-binding It is the automatic synchronization of data between        model and view components.
  2.   Scope  These are objects that refer to the model. They act as a glue between controller and view
  3.   Services AngularJS come with several built-in services for example $http to make a XMLHttpRequests. These are singleton objects which are instantiated only once in app.
  4.   Controller These are JavaScript functions that are bound to a particular scope.
  5.   Filters These select a subset of items from an array and returns a new array.
  6.   Directives Directives are markers on DOM elements (such as elements, attributes, css,and more). These can be used to create custom HTML tags that serve as new,custom widgets.AngularJS has built-in directives (ngBind, ngModel...)
  7.   Templates These are the rendered view with information from the controller and model.These can be a single file (like index.html) or multiple views in one page using "partials"
  8.   Routing  It is concept of switching views.
  9.   Deep Linking Deep linking allows you to encode the state of application in the URL so that it can be bookmarked. The application can then be restored from the URL to the same state.
  10.   Dependency Injection AngularJS has a built-in dependency injection subsystem that helps the developer by making the application easier to develop, understand, and test

What are controllers and need of ng-controller and ng-model in AngularJS?
Controllers are simple javascript function which provides data and logic to HTML UI.
As the name says controller they control how data flows from the server to HTML UI.

For example below is simple “Customer” controller which provides data via “CustomerName” and “CustomerCode” property 

and Add/ Update logic to save the data to database.

function Customer($scope)


        $scope.CustomerName = "Shiv";

        $scope.CustomerCode = "1001";

        $scope.Add = function () {


        $scope.Update = function () {



“ng-controller” is a directive.Controllers are attached to the HTML UI by using the “ng-controller” 
directive tag and the properties of the controller are attached by using “ng-model” directive. 
For example below is a simple HTML UI which is attached to the “Customer” controller via the “ng-controller” 
directive and the properties are binded using “ng-model” directive.

<div ng-controller="Customer">

<input type=text id="CustomerName"  ng-model="CustomerName"/><br />

<input type=text id="CustomerCode"  ng-model="CustomerCode"/>


What is $scope in AngularJS?

'$scope' is an object instance of a controller. 

'$scope' object instance get’s created when 'ng-controller' directive is encountered.
Every controller has an associated $scope object
A controller (constructor) function is responsible for setting model properties and functions. 
This can be done only through $scope. Whatever function or model you apply in View (html file), 
that is accessed in controller using scope.

Only methods defined on this $scope object are accessible from the HTML/view. 

Example - from ng-click, filters, etc.

Now See the below examples –

 function MyController($scope) {

 $scope.username = 'World';


In the above example we are defining any attribute named username with its value as “World”. 

Suppose in the html file you have the following line of code –

<div ng-controller="MyController">


This will automatically pick up the value from controller and display it on screen.

Now see below example

$scope.sayHello = function() {

$scope.greeting = 'Hello ' + $scope.username + '!';


This is a function defined in a controller which you can access in view by following code –

<div ng-controller="MyController">


Here, data.greeting will automatically pick value from sayHello function 

i.e. the value displayed will be "Hello World". "World" from username concatenated with “Hello” before.

What is the ng-app directive in AngularJS

ng-app directive starts an AngularJS Application. It defines the root element. 
It automatically initializes or bootstraps the application when web page containing AngularJS Application is loaded. 
It is also used to load various AngularJS modules in AngularJS Application. 

In following example, we've defined a default AngularJS application using ng-app attribute of a div element.

<div ng-app="">

What is the ng-init in AngularJS 

ng-init directive initializes an AngularJS Application data. 
It is used to put values to the variables to be used in the application.
In following example, we'll initialize an array of countries. We're using JSON syntax to define array of countries.

<div ng-app = "" ng-init = "countries = [{locale:'en-US',name:'United States'}, {locale:'en-GB',name:'United Kingdom'},

What is the ng-model directive in AngularJS

ng-model directive defines the model/variable to be used in AngularJS Application. 
In following example, we've defined a model named "name".

<div ng-app = "">
   <p>Enter your Name: <input type = "text" ng-model = "name"></p>

What is the ng-repeat directive

ng-repeat directive repeats html elements for each item in a collection. 
In following example, we've iterated over array of countries.

<div ng-app = "">
   <p>List of Countries with locale:</p>
      <li ng-repeat = "country in countries">
         {{ 'Country: ' + country.name + ', Locale: ' + country.locale }}

AngularJS Interview Question       AngularJS Interview Question -1


Post a Comment