How to use custom commands in AngularJS

Source: Internet
Author: User

How to use custom commands in AngularJS

This article describes how to use custom commands in AngularJS, including using custom HTML tags. For more information, see

Use AngularJS in custom commands to expand HTML functions. Define the "command" function used by custom commands. The custom command only replaces the activated element. The AngularJS application finds matching elements during the guidance process, and completes the use of the custom command compile () method for one-time activity and re-processing using the command-based range custom command link () method element. AngularJS supports creating custom commands based on the types of the following columns of elements.

Element ctictives-when the command encounters, a matching Element is activated.

Attribute--a matching Attribute is activated when the command encounters.

CSS--When the command encounters, the matching CSS style is activated.

The Comment--command activates the matching annotation when it encounters.

Understand custom commands

Define custom HTML tags.

?

1

2

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

<Student name = "Piyush"> </student>

Define custom commands to process the preceding custom HTML tags.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

Var mainApp = angular. module ("mainApp", []);

 

// Create a directive, first parameter is the html element to be attached.

// We are attaching student html tag.

// This directive will be activated as soon as any student element is encountered in html

MainApp. directive ('student ', function (){

// 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> ";

// Scope is used to distinguish each student element based on criteria.

Directive. scope = {

Student: "= name"

}

// Compile is called during application initialization. AngularJS callit once when html page is loaded.

Directive. compile = function (element, attributes ){

Element.css ("border", "1px solid # cccccc ");

// LinkFunction is linked with each element with 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 take the update range as the instruction. Here, we use the name attribute value as the sub-scope.

?

1

2

3

4

5

6

7

8

9

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

<Html>

<Head>

<Title> Angular JS Custom ctives ves </title>

</Head>

<Body>

<H2> AngularJS Sample Application

<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>

Result

Open textangularjs.html in the webbrowser. The result is as follows:

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.