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", []);
Create a directive, parameter 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 Directive.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 with each element with scopeLement 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
Results
Open textangularjs.html in your Web browser. See the results as follows:
The above is the ANGULARJS custom instruction data collation, follow-up continue to add, thank you for your support of this site!