Angularjs interview questions:1

What is AngularJS?

AngularJS is a JavaScript framework to develop high performance web application by simplifying binding JavaScript objects with HTML elements. It provides option to develop client side development using JavaSript in MVC.

How to initialize AngularJS applicaiton data?

By using ng-init directive we can initialize the AngularJS app data.

<div ng-app="myApp" ng-controller="myCntrl" ng-init="Qty=5;UnitPrice=6;"> </div>
<div ng-app="myApp" ng-controller="myCntrl" ng-init="initval()"> </div> <script language="javascript"> var myapp = angular.module("myApp", []); myapp.controller("myCntrl", function ($scope) { $scope.initval = function(){ $scope.Qty = 5; $scope.UnitPrice = 6; } }); </script>
See more about ng-init /angularjs/nginit

What are the different custom directive types in AngularJS

There are 4 different types of custom directive in AngularJS

  • Element directives(E)
  • Attribute directives(A)
  • CSS class directives(C)
  • Common directives(M)
By using directive restrict property we can restrict the custom directive availability.
myApp.directive("customdir", function(){ var dirout = {}; dirout.restrict = 'A'; //directive implementaiton });
when directive restricted for Attribute level (A), then you can use the directive as
<div customdir></div>

But you cannot use it like an elements

What is the use of ng-repeat directive

We can use ng-repeat directive to repeat the html elements for each item in a collection. For example if you want to display the students list in a table or different coursed in a list element you can use ng-directive. Example

<table> <thead> <tr> <th>Student Name</th> </tr> </thead> <tbody> <tr ng-repeat="student in studentlist""> <td>{{ student.Name}}</td> </tr> </tbody> </table>
<ul> <li ng-repeat="course in courselist">{{ course }}</li> </ul>
See more about ng-repeat /angularjs/ngrepeat

Explain about expressions in AngularJS

  • AngularJS expressions are used to bind data to HTML like ng-bind directive
  • AngularJS expressions are similar to JavaScript expressions
  • Expressions are written inside double braces {{ expression }}
  • Expression can be written as one time binding
  • You can write expressions on
    • Numbers
    • Strings
    • Objects
    • Arrays
See more about AngularJS expression /angularjs/expressions