12

I'm facing to an issue with AngularJS - I'm not able to display the selected value in a <select>.

Here is my use case:

I have a view which starts by an ng-repeat to display components. Each component contains a select to choose the vat rate. When creating new items, it looks fine. But when I'm editing existing items, the actual vatRate code is not displayed in the select, and instead I see the default option "-- Select VAT Rate --" instead of the selected VAT.

My model only contains the Id of the vat Rate.

With a single component I can use a variable in the $scope to set the current item value, but here I can have multiple components, each of them has its own vat rate, so I am not sure how do that here:

Here is my code

<div ng-repeat="i in items">
   <label>{{i.id}}</label>
   <input ng-model="i.title">
   <select ng-model="i.vatRateId">
      <option value="">--- Select an option ---</option>
      <option ng-repeat="value in vatRates"
              ng-selected="i.vatRateId == value.id"
              value="{{value.id}}">{{value.value}}     
      </option>
   </select>
</div>

And the objects:

$scope.vatRates = [
    { 'id': 1, 'value' : '20' },
    { 'id': 2, 'value' : '10' },
    { 'id': 3, 'value' : '7' }
];

$scope.items = [
    { 'id': 1, 'title' : 'Title1', 'vatRateId' : '1' },
    { 'id': 2, 'title' : 'Title2', 'vatRateId' : '2' },
    { 'id': 3, 'title' : 'Title3', 'vatRateId' : '3' }
];

Here is a live demo to explain my issue:

Demo on PLNKR

I'm not able to set to each item in the select the right value.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Pat
  • 329
  • 1
  • 3
  • 11
  • Sure, we could try to help you. But can you help us first? Help us by removing the irrelevant parts of the markup and styling? Then, to avoid guesses and assumptions, give us an idea of the structure of some of the objects you use and some functions, like `updateVatRate` or `settings`? For extra bonus, maybe translate some variables/text into English (seeing how this is an English-language site). I fixed some things for you - please add more details about the missing code and next time, please invest time in your question. – New Dev May 07 '15 at 06:18
  • Thanks @NewDev, I thought I was clear enought ^^ nevermind here is a live demo to explain my issue. http://plnkr.co/edit/OROLI64Q13PlJhSE4GOl?p=preview I'm not able to set to each item in the select the right value – Pat May 07 '15 at 06:49
  • it's good that you have a demo, but the question should stand on its own - I updated it, fix the edit if you feel it doesn't reflect your actual issue. – New Dev May 07 '15 at 06:58
  • The accept answer has too many problems to be useful to other readers. – georgeawg Jul 02 '18 at 23:29

4 Answers4

27

I am not very much clear about your requirement but if you want to display a default selected value to <select>, you can use ng-selected. In order to use that you need to have default selected value in your controller as a model to your <select> and add ng-selected to your <options ng-selected="{{defaultvalue == value.id}}" ng-repeat="value in vatRates">

For code and reference i just altered your plunker,

http://embed.plnkr.co/ZXo8n0jE8r3LsgdhR0QP/preview

Hope this helps.

Alhuck
  • 1,029
  • 8
  • 11
  • Happy that your issue got solved!! :) – Alhuck May 07 '15 at 10:39
  • 1
    Just in case you are setting the default to a known string value, such as "Matt", you can't just use `ng-selected="'Matt'"`. Your known string needs to be equated to the value, i.e. `ng-selected="{{ value == 'Matt' }}"` – Matt Jan 27 '16 at 20:13
  • If you are using `ngModel` on the select, you should not use `ngSelected` on the options, as `ngModel` will set the select value and selected options. See [AngularJS ng-selected Directive API Reference](https://docs.angularjs.org/api/ng/directive/ngSelected). – georgeawg Jul 02 '18 at 23:14
  • The `ng-selected` attribute should not have interpolation with curly brackets `{{ }}`. See [AngularJS Developer Guide - Binding to boolean attributes](https://docs.angularjs.org/guide/interpolation#binding-to-boolean-attributes). See also [AngularJS Developer Guide - Why mixing interpolation and expressions is bad practice](https://docs.angularjs.org/guide/interpolation#why-mixing-interpolation-and-expressions-is-bad-practice-). – georgeawg Jul 02 '18 at 23:23
9

You should set the default value on the ng-model via controller instead of ng-init. From the docs

The only appropriate use of ngInit is for aliasing special properties of ngRepeat, as seen in the demo below. Besides this case, you should use controllers rather than ngInit to initialize values on a scope.

— AngularJS ng-init Directive API Reference - Overview

angular.module('app', [])
.controller('SelectController', function() {

    var selectCtrl = this;
  selectCtrl.values = ["Value 1", "Value 2", "Value 3"];
  selectCtrl.selectedValue = "Value 1";
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="SelectController as selectCtrl">
<select ng-model="selectCtrl.selectedValue"> 
  <option ng-repeat="value in selectCtrl.values" value="{{value}}">{{value}}     </option> 
  </select>
</div>
Community
  • 1
  • 1
kachhalimbu
  • 2,190
  • 17
  • 14
2
<select ng-model="val" ng-init="myval = vals[0]">
    <option ng-repeat="myval in vals" value="{{myval.value}}">{{val.name}}
    </option>
</select>
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Adam Michalski
  • 1,722
  • 1
  • 17
  • 38
  • For more information, see [AngularJS API Reference - Using ngRepeat to generate select options](https://docs.angularjs.org/api/ng/directive/select#using-ngrepeat-to-generate-select-options). – georgeawg Jul 03 '18 at 00:22
2

This is the only way i managed to do it, and its done when another select is changing. (maybe thats the cause other solutions don't work, please note the extra ""+ to force it to be a string. (otherwise not working)

In the controller:

$scope.formData.number_of_persons = "̲"̲+̲ $scope.numberOfPersons[0].val;

HTML:

<select name="number_of_persons" 
        id="number_of_persons" 
        ng-model="formData.number_of_persons" 
        required>
    <option ng-repeat="option in numberOfPersons" value="{{option.val}}">
        {{option.val}}
    </option>
</select>
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Chris
  • 67
  • 1
  • 7
  • 1
    The other option is to use the `ng-value` directive which allows the value to be set to something other than a string. – georgeawg Jul 03 '18 at 00:17
  • See [AngularJS `select` Directive API Reference - Using `ngValue` to bind the model to an array of objects](https://docs.angularjs.org/api/ng/directive/select#using-ngvalue-to-bind-the-model-to-an-array-of-objects). – georgeawg Jul 03 '18 at 00:21