Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...