ANGULARJS Custom instructions and sample code _ANGULARJS

Source: Internet
Author: User
Tags html tags

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!

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.