Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

html select - Angularjs ngOption with array

I want to add an html select with options AM,PM with angularjs, what i need is having the key and the value of the option be the same :

<option value="AM">AM</option>
<option value="PM">PM</option>

My html looks like this

<select ng-model="ampm" ng-options="k as v for (k , v) in ampms"></select>

and my controller looks like

  $scope.ampm = (new Date().getHours()) >= 12 ? 'PM' : 'AM';
  $scope.ampms ={"AM":"AM","PM":"PM"};

and every thing working fine.

My question is why i cant have the same thing when i used an array (i tried all the options in the ng-options) as this

$scope.ampms =["AM","PM"];

what ever i do i always get this

<option value="0">AM</option>
<option value="1">PM</option>

What i want is using an array like above with the option has the key and the value the same.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With AngularJS, you don't need to worry about what the value of the option is. All the selects I've seen with ng-options have values of 0 through whatever. If you're just looking for a dropdown with the two options, it can be as simple as

<select ng-model="ampm" ng-options="currOption for currOption in ['AM', 'PM']"></select>

See http://jsfiddle.net/EyBVN/1/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...