Detailed explanation of the use of custom directives in Angularjs

Source: Internet
Author: User
Tags html tags


This article mainly introduces the use of custom instructions in the detailed angularjs, including the use of custom HTML tags, you need friends can refer to the following



The ability to use ANGULARJS to extend HTML in custom directives. The functional definition of the "Directive" used by the custom directive. The custom directive simply replaces the element to which it is activated. During the boot process, the ANGULARJS application finds the matching elements and completes the elements of the custom instruction link () method using the instruction-based range custom instruction compile () method once active and then processed. ANGULARJS provides support for creating custom directives with the type of the following elements.



Element directives-a matching element is activated when the instruction encounters.



Attribute--When the instruction encounters, activates a matching property.



CSS--When the instruction encounters, activates the matching CSS style.



Comment--Activates the matching annotation when the instruction encounters.



Learn about custom directives



Defines a custom HTML tag.




<student name= "Mahesh" ></student><br/> <student name= "Piyush" ></student>


Define custom directives to handle the above custom HTML tags.




var Mainapp = Angular.module ("Mainapp", []);  //creat e a directive, the parameter of the HTML element to be attached. We are attaching student HTML tag. This directive would be activated as soon as any student the element is encountered in HTML mainapp.directive (' Student ', func tion () {//define the directive object var directive = {};//restrict = E, signifies that Directive is Element Directive D irective.restrict = ' E '; Template replaces the complete element with its text. Directive.template = "Student: <b>{{student.name}}</b>, Roll No: <b>{{student.rollno}}</b>"; The scope is used to distinguish the student element based on criteria. Directive.scope = {student: "=name"}//compile is called during. Application initialization. Angularjs calls it once when the HTML page is LOaded. Directive.compile = function (element, attributes) {element.css ("border", "1px solid #cccccc");//linkfunction is linked W ith each of the element with the scope to get the element specific data. var linkfunction = function ($scope, element, attributes) {element.html ("Student: <b>" + $scope. Student.name + "< /b>, Roll No: <b> "+ $scope. student.rollno+" </b><br/> "); Element.css ("Background-color", "#ff00ff"); return linkfunction; return directive; });


Define the controller to update the scope as an instruction. Here, we use the Name property value as the child's scope.




Mainapp.controller (' Studentcontroller ', function ($scope) {$scope. Mahesh = {}; $scope. Mahesh.name = "Mahesh Parashar"; $scope.   Mahesh.rollno = 1; $scope. Piyush = {}; $scope. Piyush.name = "Piyush Parashar"; $scope. Piyush.rollno = 2; });


Example



<html> <head> <title>Angular JS Custom Directives</title> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app="mainApp" ng-controller="StudentController"> <student name="Mahesh"></student><br/> <student name="Piyush"></student> </div> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> <script> var mainApp = angular.module("mainApp", []);   mainApp.directive('student', function() { var directive = {}; directive.restrict = 'E'; directive.template = "Student: <b>{{student.name}}</b> , Roll No: <b>{{student.rollno}}</b>";   directive.scope = { student : "=name" }   directive.compile = function(element, attributes) { element.css("border", "1px solid #cccccc");   var linkFunction = function($scope, element, attributes) { element.html("Student: <b>"+$scope.student.name +"</b> , Roll No: <b>"+$scope.student.rollno+"</b><br/>"); element.css("background-color", "#ff00ff"); }   return linkFunction; }   return directive; });   mainApp.controller('StudentController', function($scope) { $scope.Mahesh = {}; $scope.Mahesh.name = "Mahesh Parashar"; $scope.Mahesh.rollno = 1;   $scope.Piyush = {}; $scope.Piyush.name = "Piyush Parashar"; $scope.Piyush.rollno = 2; });   </script> </body> </html>


Results



Open textangularjs.html in your Web browser. See the results as follows:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.