Step 2-AngularJs Directives

Definition – AngularJS directive adds angularJS functionalities to HTML.

or

AngularJs Directives introduces AngularJS to HTML.

or

AngularJs Directives enables AngularJs to work with HTML.

or

AngularJs Directives allows HTML to extend with AngularJS attributes.

Syntax – ng-

AngularJS Directive-  There are total 55 Directives and we can also create our own directives.
Most Useful directives are following :

  • ng-app: Starts an AngularJS Application.
  • ng-init: Initializes data.
  • ng-model: A variable to be used in AngularJS.
  • ng-repeat: Repeats for each item in a collection.

Now look on below example –

<!DOCTYPE html>
<html ng-app="">
<body>
  <h1>AngularJS Directives Example</h1>
  <div ng-init="names=['Alice', 'Bob', 'Charlie']">
    <input type="text" ng-model="newName">
    <button ng-click="names.push(newName)">Add Name</button>
    <ul>
      <li ng-repeat="name in names">{{name}}</li>
    </ul>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</body>
</html>

Note – Although above example is simple to understand the concept, but you can see we didn’t define controller/module etc and practically in your project you would need to add them, so now look on below example which is similar to above one but below is recommended and considered good practice.

<!DOCTYPE html>
<html ng-app="myApp">
<body ng-controller="MyController">
  <h1>AngularJS Directives Example</h1>
  <div ng-init="names=['Alice', 'Bob', 'Charlie']">
    <input type="text" ng-model="newName">
    <button ng-click="addName()">Add Name</button>
    <ul>
      <li ng-repeat="name in names">{{name}}</li>
    </ul>
  </div>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
  <script>
    var app = angular.module('myApp', []);
    app.controller('MyController', function($scope) {
      $scope.addName = function() {
        $scope.names.push($scope.newName);
        $scope.newName = '';
      };
    });
  </script>
</body>
</html>

This is list of all 55 directives below to try

DirectiveDescription
ng-appDefines the root element of an AngularJS application
ng-bindBinds the content of an HTML element to an expression
ng-bind-htmlBinds the innerHTML of an HTML element to an expression, and parses the expression for HTML content
ng-bind-templateBinds the content of an HTML element to a template
ng-blurBinds an event handler function to the blur event
ng-changeBinds an event handler function to the change event
ng-checkedBinds the checked state of a checkbox or radio button to an expression
ng-classBinds the class attribute of an HTML element to an expression
ng-class-evenBinds the class attribute of an HTML element to an expression when the element is even-indexed
ng-class-oddBinds the class attribute of an HTML element to an expression when the element is odd-indexed
ng-clickBinds an event handler function to the click event
ng-cloakHides an element until AngularJS has compiled the element
ng-controllerDefines the controller for a section of HTML
ng-copyBinds an event handler function to the copy event
ng-cspEnables/disables the Content Security Policy
ng-cutBinds an event handler function to the cut event
ng-dblclickBinds an event handler function to the dblclick event
ng-disabledBinds the disabled state of an HTML element to an expression
ng-focusBinds an event handler function to the focus event
ng-formCreates an AngularJS form element
ng-hideHides an element based on an expression
ng-hrefBinds the href attribute of an anchor element to an expression
ng-ifConditionally includes an element based on an expression
ng-includeIncludes HTML content from an external file
ng-initInitializes data
ng-keydownBinds an event handler function to the keydown event
ng-keypressBinds an event handler function to the keypress event
ng-keyupBinds an event handler function to the keyup event
ng-listBinds the value of a textarea element to an expression, as an array of strings
ng-modelBinds an input, select, or textarea element to a variable
ng-mousedownBinds an event handler function to the mousedown event
ng-mouseenterBinds an event handler function to the mouseenter event
ng-mouseleaveBinds an event handler function to the mouseleave event
ng-mousemoveBinds an event handler function to the mousemove event
ng-mouseoverBinds an event handler function to the mouseover event
ng-mouseupBinds an event handler function to the mouseup event
ng-non-bindablePrevents AngularJS from compiling the contents of an element
ng-optionsGenerates a list of <option> elements based on an expression
ng-pasteBinds an event handler function to the paste event
ng-patternValidates the input value against a regular expression pattern
ng-pluralizeConditionally pluralizes a message based on a number
ng-readonlySets the element as read-only if the expression evaluates to true
ng-repeatRepeats an HTML element for each item in a collection
ng-selectedSets the selected attribute on an option element if the expression evaluates to true
ng-showShows or hides an element based on the expression value
ng-srcBinds the source attribute of an image element to an expression
ng-styleSets inline CSS styles based on the expression value
ng-submitExecutes an expression when a form is submitted
ng-switchConditionally inserts an HTML element based on the expression value
ng-switch-whenSpecifies a case for the ng-switch directive
ng-switch-defaultSpecifies the default case for the ng-switch directive
ng-transcludeTranscludes the content of an HTML element into a directive
ng-valueBinds the value attribute of an input element to an expression
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.