How to add an our own directive in angular application.

AngularJs has a useful set of built in directives.They offer a useful set of functionalities for our angular application. What if we want to add a different directive

A new directive can be add using directive() method as same as adding a controller
  • Following code displays how to add a directive and use it our application.
<!DOCTYPE html>
<html>
 <script src="scripts/angular.min.js"></script>
 <body>

  <div ng-app="myApp" my-directive></div>

  <script>
   var app = angular.module("myApp", []);
   app.directive("myDirective", function() {
    return {
        template : "This is a my own directive"
    };
   });
  </script>

 </body>
</html>




Note: Be careful when you naming your directive. If you use UPPERCASE letters in the middle of name make sure use all simple letters after separating by a "-" when using that directive  
      Ex: directive name - myDirective
             When using      - my-directive.


No comments:

Post a Comment