AngularJS學習筆記之ng-options指令

來源:互聯網
上載者:User

AngularJS學習筆記之ng-options指令

       ng-options是angular-1.3新出的一個指令,這篇文章就來介紹這個指令的用法.有需要的小夥伴可以參考下。

 

 

       1.基本下拉效果(lable for value in array)

  其中select標籤中的ng-model屬性必須有,其值為選中的對象或屬性值。

?

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

<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.自訂下拉顯示名稱(label for value in array)

label可以根據需要拼接出不同的字串

?

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

<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 選項分組

group by分組項

?

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

<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 自訂ngModel的綁定

下面selected的值為optData的id 效果 http://sandbox.runjs.cn/show/nhi8ubrb

?

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

<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: '黃'

}];

}]);

效果:http://runjs.cn/detail/nhi8ubrb

       以上所述就是本文的全部內容了,希望大家能夠喜歡。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.