0

Angular doesn't update select elements with the contents of ng-model. The binding is single way only select->model. How can I make changes in the model propagated to the view ?

template:

<select class="gu2" name="coverDuration" ng-model="creditLimit.coverDuration" required="required">
   <option ng-value="90" selected="selected">90 days</option>
   <option ng-value="180">180 days</option>
</select>
just to check: {{creditLimit.coverDuration}}

controller:

app.controller('CreditLimitDetailsController', function ( $scope, creditLimit /* ... */ ) {
    // creditLimit.coverDuration can be 90 or 180.
    // I use a service to store the values between successive instanciations
    $scope.creditLimit = creditLimit;
}

In this exemple, the select stays with no option select, whereas the check shows 90.

2
  • Have a look at ngOptions. This might make things a lot easier for you. Commented Dec 18, 2013 at 11:06
  • I see a lot of answer with ngOptions, but this seems overkill and confusive for just two static options :/. Seriously, how can such a simple thing not be included in angular ? Commented Dec 18, 2013 at 11:26

3 Answers 3

2

It works:

app.controller('MainCtrl', function($scope) {
  $scope.creditLimit = {}; //needs to be initialized
  $scope.creditLimit.coverDuration = 90;
});

and use

value="" 

instead

ng-value=""

Plunker

Sign up to request clarification or add additional context in comments.

3 Comments

it does not work for me(chrome 31.0.1650.63), which browser are you using ?
@BiAiB I am on 31.0.1650.63 too. I made a little edit. It works now. Use value instead ng-value.
that worked, while keeping ng-value. I need the value to be an integer because it will then be json serialized and the deseralizer won't allow strings.
0

If you don't want to use ngOptions then just use ngSelected instead of selected:

<option ng-value="90" ng-selected="true">90 days</option>

fiddle

1 Comment

I edited the question. coverDuration is variable and can be 90 or 180 (in fact it's fed by this select among different instances of this controller). I think I could work around your example with ng-selected=" creditLimit.coverDuration==90 "
0

Probably need to blur the elemenet after selection, this is browser dependent

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.