AngularJS HTML DOM
AngularJS provides instructions for binding application data to attributes of html dom elements. Today, we will introduce some instructions related to html dom operations in AngularJS.
Ng-options
In AngularJS, we can use the ng-option command to create a drop-down list. list items are output cyclically through objects and arrays. The sample code is as follows:
<Script src = "js/angular. min. js "> </script> <script type =" text/javascript "> angular. module ("myApp", []). controller ("myCtrl", function ($ scope) {$ scope. items = ["red", "blue", "green"] ;}); </script>
This will display a drop-down list. Friends who have read the previous blog should know that there isng-repeat
The command is used to repeatedly create elements, so we useng-repeat
To achieve the same effect, the Code is as follows:
<Script src = "js/angular. min. js "> </script> <script type =" text/javascript "> angular. module ("myApp", []). controller ("myCtrl", function ($ scope) {$ scope. items = ["red", "blue", "green"] ;}); </script>
{{item}}
This code can also display a drop-down selection box. Which of the two is better?
ng-repeat
The command uses an array to loop through HTML code to create a drop-down list,ng-options
This command is more suitable for creating drop-down lists.ng-options
Data can be objects, whileng-repeat
Is a string ., When the data used to create a drop-down selection box is an object,ng-options
The advantage is especially obvious. The following code demonstrates how:
<Script src = "js/angular. min. js "> </script> <script type =" text/javascript "> angular. module ("myApp", []). controller ("myCtrl", function ($ scope) {$ scope. items = {red: {r: 255, g: 0, B: 0}, green: {r: 0, g: 255, B: 0}, blue: {r: 0, g: 0, B: 255 }}); </script>
{{selectColor}}
Ng-disabled
ng-disabled
Command to directly bind application data to the disabled attribute of HTML.
Sample Code:
<Script src = "js/angular. min. js "> </script> <script type =" text/javascript "> angular. module ("myApp", []). controller ("myCtrl", function ($ scope) {}); </script>
Ng-show and ng-hide
ng-show
Command to hide or display an HTML element.ng-hide
Commands are also used to hide or display HTML elements. Andng-show
On the contrary.
Sample Code:
<Script src = "js/angular. min. js "> </script> <script type =" text/javascript "> angular. module ("myApp", []). controller ("myCtrl", function ($ scope) {}); </script>