2

I have an angular controller as written below;

controllers.controller('editCodeReviewCtrl', ($scope, $rootScope, $modalInstance, User, reviewRequestId, reviewRequestDetail, reviewRequestValue, reviewRequestTitle) -> 
  console.log "reviewRequestvalue", reviewRequestValue
  $scope.reviewRequest = {}
  $scope.reviewRequest.id = reviewRequestId
  $scope.reviewRequest.detail = reviewRequestDetail
  $scope.reviewRequest.value = '25.0'
  $scope.reviewRequest.title = reviewRequestTitle
  $scope.values = [
    {value: '10.0'},
    {value: '25.0'},
    {value: '50.0'} ]
)

And my view template looks like the below;

    <select ng-model="reviewRequest.value" ng-options="ele.value for ele in values" name="value" required class="friendly-margin pull-right">
    </select>

I would expect that my drop down would have default value set, in the example case here it should be set to '25.0' but I'm not observing that behavior in the browser.

notice the html that is being output

<select ng-model="reviewRequest.value" ng-options="ele.value for ele in values" name="value" required="" class="friendly-margin pull-right ng-pristine ng-valid ng-valid-required ng-touched">
<option value="?" selected="selected" label=""></option>
<option value="0" label="10.0">10.0</option>
<option value="1" label="25.0">25.0</option>
<option value="2" label="50.0">50.0</option>
</select>

What direction to take this debugging?

i've looked at this post as well, how to use ng-option to set default value of select element which appears consistent with how I've written my own code.

Community
  • 1
  • 1
John
  • 1,246
  • 15
  • 34

2 Answers2

4

As the documentation explains, when you use an expression such as

label for value in array

what is displayed in the select box is label, and what is bound to the ngModel is value. So, in your expression:

ng-model="reviewRequest.value" ng-options="ele.value for ele in values"

ele is one of the objects in the array of values. And if you want the object {value: '25.0'} to be the one selected in the select box, your ngModel should be a reference to this object (not an equal copy!):

So your controller should set reviewRequest.value to $scope.values[1].

Or, if you want the ngModel to contain the string 25.0and not the object containing this value, the expression should be

ng-options="ele.value as ele.value for ele in values"
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • JB excellent answer and I really appreciate you pointing out the underlying details that I was being tone-deaf to. Thanks. I tweeted to you, i'd like to connect via email about a product i'm working on if you are up for it. – John Feb 04 '15 at 15:38
  • Great answer.. or just use another language that's not duplicating your work load.. lol. Take a look at the question, and answer... HTML > SELECTED. It was simple.. Now it's complicated. – Luke Nov 09 '16 at 04:34
1

In controller $scope.reviewRequest.value = $scope.values[1];

Naresh Kumar
  • 276
  • 2
  • 10