1. Question:What are Directives in AngularJS? 

    Answer
    AngularJS directives are a combination of AngularJS template markups (HTML attributes or elements, or CSS classes) and supporting JavaScript code. The JavaScript directive code defines the template data and behaviors of the HTML elements.
    
    AngularJS directives are used to extend the HTML vocabulary i.e. they decorate html elements with new behaviors and help to manipulate html elements attributes in interesting way.
    
    There are some built-in directives provided by AngularJS like as ng-app, ng-controller, ng-repeat, ng-model etc.

    1. Report
  2. Question:What is the role of ng-app, ng-init and ng-model directives? 

    Answer
    The main role of these directives is explained as: 
    - ng-app - Initialize the angular app. 
    - ng-init - Initialize the angular app data. 
    - ng-model - Bind the html elements like input, select, text area to angular app model.

    1. Report
  3. Question:How to create custom directives in AngularJS? 

    Answer
    You can create your own custom directive by using following syntax:
    var app = angular.module('app', []); 
    //creating custom directive syntax app.directive("myDir", function () { return {  restrict: "E", 
    //define directive type like E = element, A = attribute, C = class, M = comment  scope: { //create a new child scope or an isolate scope 
     title: '@' //@ reads the attribute value,//= provides two-way binding, //& works with functions
     },template: "<div>{{ myName }}</div>",// define HTML markup 
       templateUrl: 'mytemplate.html', //path to the template, used by the directive    replace: true |   false, // replace original markup with template yes/no 
       transclude: true | false, // copy original HTML content yes/no 
       controller: function (scope) { //define controller, associated with the directive template //TODO: }, 
     link: function (scope, element, attrs, controller) {//define function, used for DOM manipulation //TODO: 
        } 
       } 
    });

    1. Report
  4. Question:What are different ways to invoke a directive? 

    Answer
    As an attribute
    <span my-directive></span>
    
    As a class
    <span class="my-directive: expression;"></span>
    
    As an element
    <my-directive></my-directive>
    
    As a comment
    <!-- directive: my-directive expression -->

    1. Report
  5. Question:What is restrict option in directive? 

    Answer
    The restrict option in angular directive, is used to specify how a directive will be invoked in your angular app i.e. as an attribute, class, element or comment.
    There are four valid options for restrict:
    
    'A' (Attribute)- <span my-directive></span> 
    'C' (Class)- <span class="my-directive:expression;"></span> 
    'E' (Element)- <my-directive></my-directive> 
    'M' (Comment)- <!-- directive: my-directive expression -->

    1. Report
  6. Question:Can you define multiple restrict options on a directive? 

    Answer
    You can also specify multiple restrict options to support more than one methods of directive invocation as an element or an attribute. Make sure all are specified in the restrict keyword as:
    restrict: 'EA'

    1. Report
  7. Question:What is auto bootstrap process in AngularJS? OR How AngularJS is initialized automatically? 

    Answer
    Angular initializes automatically upon DOMContentLoaded event or when the angular.js script is downloaded to the browser and the document.readyState is set to complete. At this point AngularJS looks for the ng-app directive which is the root of angular app compilation and tells about AngularJS part within DOM. When the ng-app directive is found then Angular will:
    1. Load the module associated with the directive.
    2. Create the application injector.
    3. Compile the DOM starting from the ng-app root element.
    
    This process is called auto-bootstrapping.
    
    Example 
    <html> 
     <body ng-app="myApp"> 
       <div ng-controller="Ctrl"> Hello {{msg}}! </div> 
    
      <script src="lib/angular.js"></script> 
     <script> 
       var app = angular.module('myApp', []); 
       app.controller('Ctrl', function ($scope) { $scope.msg = 'World'; }); 
     </script> 
    /body> 
    </html>

    1. Report
  8. Question:What directives are used to show and hide HTML elements in AngularJS? 

    Answer
    ng-show and ng-hide directives are used to show and hide HTML elements in the AngularJS based on an expression. When the expression is true for ng-show or ng-hide, HTML element(s) are shown or hidden from the page. When the expression is false for ng-show or ng-hide, HTML element(s) are hidden or shown on the page.
    <div ng-controller="MyCtrl"> 
      <div ng-show="data.isShow">ng-show Visible</div> 
      <div ng-hide="data.isHide">ng-hide Invisible</div> 
    </div>
    <script> 
     var app = angular.module("app", []);
     app.controller("MyCtrl", function ($scope) { 
       $scope.data = {};
       $scope.data.isShow = true; 
       $scope.data.isHide = true; 
    }); 
    </script>

    1. Report
  9. Question:Explain directives ng-if, ng-switch and ng-repeat? 

    Answer
    ng-if – This directive can add / remove HTML elements from the DOM based on an expression. If the expression is true, it add HTML elements to DOM, otherwise HTML elements are removed from the DOM.
    <div ng-controller="MyCtrl"> 
    <div ng-if="data.isVisible">ng-if Visible</div> 
    </div> 
    
    <script> 
     var app = angular.module("app", []); 
     app.controller("MyCtrl", function ($scope) {
       $scope.data = {}; 
       $scope.data.isVisible = true; 
     }); 
    </script>
    ng-switch – This directive can add / remove HTML elements from the DOM conditionally based on scope expression.
    <div ng-controller="MyCtrl"> 
      <div ng-switch on="data.case"> 
        <div ng-switch-when="1">Shown when case is 1</div> 
        <div ng-switch-when="2">Shown when case is 2</div> 
        <div ng-switch-default>Shown when case is anything else than 1 and 2</div> 
      </div> 
    </div> 
    
    
    <script> 
      var app = angular.module("app", []); 
      app.controller("MyCtrl", function ($scope) { 
        $scope.data = {}; 
        $scope.data.case = true; 
      }); 
    </script>
    ng-repeat - This directive is used to iterate over a collection of items and generate HTML from it.
    <div ng-controller="MyCtrl"> 
      <ul> 
          <li ng-repeat="name in names"> {{ name }} </li>
     </ul> 
    </div>
    
    
    <script> 
      var app = angular.module("app", []);
      app.controller("MyCtrl", function ($scope) { 
         $scope.names = ['Shailendra', 'Deepak', 'Kamal']; 
      }); 
     </script>

    1. Report
  10. Question:What are ng-repeat special variables? 

    Answer
    The ng-repeat directive has a set of special variables which you are useful while iterating the collection. These variables are as follows:
    - $index
    - $first
    - $middle
    - $last
    <html> 
      <head> 
       <script src="lib/angular.js"></script> 
       <script> 
         var app = angular.module("app", []); 
        app.controller("ctrl", function ($scope) { 
          $scope.friends = [
           { name: 'shailendra', gender: 'boy' }, 
           { name: 'kiran', gender: 'girl' }, 
           { name: 'deepak', gender: 'boy' }, 
          { name: 'pooja', gender: 'girl' } 
        ];
        }); 
    </script> 
    
     </head>
    <body ng-app="app"> 
      <div ng-controller="ctrl"> 
        ul class="example-animate-container"> 
          <li ng-repeat="friend in friends"> 
            
    [{{$index + 1}}] {{friend.name}} is a {{friend.gender}}. <span ng-if="$first"> (first element found) </span> <span ng-if="$middle"> (middle element found) </span> <span ng-if="$last"> (last element found) </span>
    </li> </ul> </div> </body> </html>
    The $index contains the index of the element being iterated. The $first, $middle and $last returns a boolean value depending on whether the current item is the first, middle or last element in the collection being iterated.

    1. Report
Copyright © 2024. Powered by Intellect Software Ltd