Home >Web Front-end >JS Tutorial >AngularJS study notes ng-options directive_AngularJS

AngularJS study notes ng-options directive_AngularJS

WBOY
WBOYOriginal
2016-05-16 15:54:481271browse

1. Basic drop-down effect (lable for value in array)

 The ng-model attribute in the select tag must be present, and its value is the selected object or attribute value.

<div ng-controller="ngselect">
  <p>usage:label for value in array</p>
  <p>选项,{{selected}}</p>
  <select ng-model="selected" ng-options="o.id for o in optData">
    <option value="">-- 请选择 --</option>
  </select>
</div>
m1.controller("ngselect",['$scope',function($sc){
  $sc.selected = '';
  $sc.optData = [{
    id: 10001,
    MainCategory: '男',
    ProductName: '水洗T恤',
    ProductColor: '白'
  },{
    id: 10002,
    MainCategory: '女',
    ProductName: '圓領短袖',
    ProductColor: '黃'
  },{
    id: 10003,
    MainCategory: '女',
    ProductName: '圓領短袖',
    ProductColor: '黃'
  }];
}]);

2. Customize the drop-down display name (label for value in array)

label can be spliced ​​into different strings as needed

<div ng-controller="ngselect2">
  <p>usage:label for value in array(label可以根据需求拼接出不同的字符串)</p>
  <p>选项,{{selected}}</p>
  <select ng-model="selected" ng-options="(o.ProductColor+'-'+o.ProductName) for o in optData">
    <option value="">-- 请选择 --</option>
  </select>
</div>
m1.controller("ngselect2",['$scope',function($sc){
  $sc.selected = '';
  $sc.optData = [{
    id: 10001,
    MainCategory: '男',
    ProductName: '水洗T恤',
    ProductColor: '白'
  },{
    id: 10002,
    MainCategory: '女',
    ProductName: '圓領短袖',
    ProductColor: '黃'
  },{
    id: 10003,
    MainCategory: '女',
    ProductName: '圓領短袖',
    ProductColor: '黃'
  }];
}]);

3.ng-options option grouping

group by group item

<div ng-controller="ngselect3">
  <p>usage:label group by groupName for value in array</p>
  <p>选项,{{selected}}</p>
  <select ng-model="selected" ng-options="(o.ProductColor+'-'+o.ProductName) group by o.MainCategory for o in optData">
    <option value="">-- 请选择 --</option>
  </select>
</div>
m1.controller("ngselect3",['$scope',function($sc){
  $sc.selected = '';
  $sc.optData = [{
    id: 10001,
    MainCategory: '男',
    ProductName: '水洗T恤',
    ProductColor: '白'
  },{
    id: 10002,
    MainCategory: '女',
    ProductName: '圓領长袖',
    ProductColor: '黃'
  },{
    id: 10003,
    MainCategory: '女',
    ProductName: '圓領短袖',
    ProductColor: '黃'
  }];
}]);

4.ng-options Customize ngModel binding

The selected value below is the id effect of optData http://sandbox.runjs.cn/show/nhi8ubrb

<div ng-controller="ngselect4">
  <p>usage:select as label for value in array</p>
  <p>选项,{{selected}}</p>
  <select ng-model="selected" ng-options="o.id as o.ProductName for o in optData">
    <option value="">-- 请选择 --</option>
  </select>
</div>
m1.controller("ngselect4",['$scope',function($sc){
  $sc.selected = '';
  $sc.optData = [{
    id: 10001,
    MainCategory: '男',
    ProductName: '水洗T恤',
    ProductColor: '白'
  },{
    id: 10002,
    MainCategory: '女',
    ProductName: '圓領长袖',
    ProductColor: '黃'
  },{
    id: 10003,
    MainCategory: '女',
    ProductName: '圓領短袖',
    ProductColor: '黃'
  }];
}]);

Effect:http://runjs.cn/detail/nhi8ubrb

The above is the entire content of this article, I hope you all like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn