Home >Web Front-end >JS Tutorial >How to use the angular directive ng-options
ng-options directive purpose:
Use an array or object in an expression to automatically generate a list of options in a select. ng-options are very similar to ng-repeat. In many cases, ng-repeat can be used instead of ng-options. But ng-options provide some benefits, such as reducing memory and improving speed, and providing selection box options for users to choose. When an option is selected in select, the option will be automatically bound to the corresponding data using ng-model. If you want to set a default value, you can do it like this: $scope.selected = $scope.collection[3].
This article mainly introduces to you how to use angular instruction note ng-options. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.
1.1 The purpose of track by:
track by is mainly to prevent duplication of values, and angularjs will report an error. Because angularjs needs a unique value to bind to the generated dom to facilitate data tracking. For example: items=["a","a","b"], so ng-repeat="item in items" will cause an error, and use ng-repeat="(key, value) in items track by key" There will be no errors.
1.2 Notes on using ng-option
When using it, you must add the ng-model directive, otherwise an error will be reported if it cannot be used
2. What do label and value represent in the select drop-down box?
First write the simplest and most original select drop-down box
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>label 和 value 具体是什么</title> </head> <body> <select> <!-- value 是存储到数据库中的值,label是显示在页面上的值 value 就是 1、2、3、4这些数值; lable 是"语文" “数学”这些 --> <option value="1">语文</option> <option value="2">数学</option> <option value="3">英语</option> <option value="4">生物</option> </select> </body> </html>
Now introduce angular to use ng-options command to generate a drop-down box, look at the code to generate the page
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>label 和 value 具体是什么</title> <script type="text/javascript" src="../js/angular-1.3.0.js"></script> </head> <body ng-app="myapp"> <p ng-controller="mainCtrl"> <select> <!-- value 是存储到数据库中的值,label是显示在页面上的值 value 就是 1、2、3、4这些数值; lable 是"语文" “数学”这些 --> <option value="1">语文</option> <option value="2">数学</option> <option value="3">英语</option> <option value="4">生物</option> </select> <br> <br> <br> <p>{{ selectedCity }} <br> <!-- 这里 c.id as c.city for c in obj 我们使用 obj 对象的 id作为select的value,使用obj 的city 作为 select 的label --> <select ng-options="c.id as c.city for c in obj" ng-model="selectedCity"> </select> </p> </p> <script type="text/javascript"> var myapp = angular.module('myapp', []); myapp.controller('mainCtrl', ['$scope', function($scope) { $scope.selectedCity = "bj"; $scope.obj = [ { "id": "bj", "city": "北京" }, { "id": "sh", "city": "上海" }, { "id": "zz", "city": "郑州" } ]; }]) </script> </body> </html>
Look at the preview page effect, and add the select generated using ng-options later , we use the id of the obj object as the value of the select, and the city of obj as the label of the select
3. Three common methods of ng-options:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>label 和 value 具体是什么</title> <script type="text/javascript" src="../js/angular-1.3.0.js"></script> <style type="text/css"> .mart30 { margin-top: 30px; border-top: 1px solid #000; } </style> </head> <body ng-app="myapp"> <p ng-controller="mainCtrl"> <select> <!-- value 是存储到数据库中的值,label是显示在页面上的值 value 就是 1、2、3、4这些数值; lable 是"语文" “数学”这些 --> <option value="1">语文</option> <option value="2">数学</option> <option value="3">英语</option> <option value="4">生物</option> </select> <p class="mart30"> <h3>演示 label 和 value 值的变化</h3> {{ selectedCity }} <!-- 这里 c.id as c.city for c in obj 我们使用 obj 对象的 id作为select的value,使用obj 的city 作为 select 的label --> <select ng-options="c.id as c.city for c in obj1" ng-model="selectedCity"> </select> </p> <p class="mart30"> <h3>1. “数组”实现基本下拉</h3> <p>语法: laber for value in array</p> <select ng-options="animal for animal in arr1" ng-model="selectedAnimal"></select> <br> </p> <p class="mart30"> <h3>2. “包含对象的数组”实现“label 和 value值不同”的下拉</h3> <p>语法: select as label for value in array</p> <p>哪位同学你认识?你的选择是:{{selectedStu}}</p> <select ng-options="c.name as c.id for c in obj2" ng-model="selectedStu"></select> <br> <br> <br> <p><strong>自定义下拉显示内容格式</strong></p> <p>哪位同学你认识?你的选择是:{{selectedStuString}}</p> <p>语法:拼接字符串</p> <select ng-options="c.name as (c.name +'- 英文名:'+c.id) for c in obj2" ng-model="selectedStuString"></select> <br> <br> <br> <p><strong>使用group by对下拉菜单分组</strong></p> <p>语法:label group by groupName for value in array</p> <p>哪位同学你认识?你的选择是:{{selectedStuString2}}</p> <select ng-options="c.name group by c.sex for c in obj2" ng-model="selectedStuString2"></select> </p> <p class="mart30"> <h3>3. “对象”实现基本下拉</h3> <p>语法 1: label for (key , value) in object</p> <p>哪个城市?你的选择是:{{scity}}</p> <select ng-options="key for (key , value) in obj3" ng-model="scity"></select> <p>语法 2: select as label for (key ,value) in object</p> <p>哪个城市?你的选择是:{{scity01}}</p> <select ng-options="value as key for (key , value) in obj3" ng-model="scity01"></select> </p> </p> <script type="text/javascript"> var myapp = angular.module('myapp', []); myapp.controller('mainCtrl', ['$scope', function($scope) { //定义包含对象的数组 obj1 $scope.obj1 = [ { "id": "bj", "city": "北京" }, { "id": "sh", "city": "上海" }, { "id": "zz", "city": "郑州" } ]; $scope.selectedCity = "bj"; // 定义数组 $scope.arr1 = ["大白", "阿狸", "熊猫"]; //定义默认为 “大白” $scope.selectedAnimal = "大白"; //定义包含对象的数组 obj2 $scope.obj2 = [ { "id": "lilei", "name": "李雷", "sex": "man" }, { "id": "hanmeimei", "name": "韩梅梅", "sex": "woman" }, { "id": "jack", "name": "杰克", "sex": "man" } ]; $scope.selectedStu = "韩梅梅"; //定义简单对象 obj3 $scope.obj3 = { "湖北": "鄂", "广东": "粤", "河南": "豫" }; }]) </script> </body> </html>
A little explanation about key and value in the object usage method
4, ng -options All usage supplements
The red part has examples in the code, the rest please digest and understand the test by yourself
For arrays:
label for value in array
select as label for value in array
label group by group for value in array
label disable when disable for value in array
label group by group for value in array track by trackexpr
label disable when disable for value in array track by trackexpr
label for value in array | orderBy: orderexpr track by trackexpr(for including a filter with track by)
for object:
label for (key , value) in object
select as label for (key,value) in object
label group by group for (key,value) in object
label disable when disable for (key, value) in object
select as label group by group for (key, value) in object
Detailed explanation of the ng-options directive of AngularJS
The difference between ng-repeat and ng-options
AngularJS is implemented in ng-Options Solution to index
The above is the detailed content of How to use the angular directive ng-options. For more information, please follow other related articles on the PHP Chinese website!