0

I have this html markup:

    <div ng-repeat="prop in props" style="margin-bottom: 10px;">
  <label class="col-md-4 control-label">Property {{$index + 1}} ({{prop.AddressLine1}})</label><div class="col-md-8">
       <select ng-model="prop.Grade" class="form-control" ng-options="opt.name for opt in propGradings track by opt.id">                                                                       <option ng-selected="{{option.id == prop.Grade}}" ng-repeat="option in propGradings" ng-value="{{option.id}}">{{option.name}}</option>     </select>
   </div>
 </div>

This static array to fill in the dropdown:

$scope.propGradings = [{ name: "1", id: 1 }, { name: "2", id: 2 }, { name: "3", id: 3 }, { name: "4", id: 4 }];

I'm able to load the items in the dropdown, but I'm not able to preselect the correct value based on the prop.Grade value.

HTML Output:

enter image description here

Any idea what am I doing wrong?

9
  • Does prop.Grade have a value? This kind of thing usually happens if the ng-model value is undefined, or is a non-string value. Commented Nov 29, 2016 at 18:58
  • What value are you putting into prop.Grade? obj or id? Commented Nov 29, 2016 at 19:03
  • @mhodges its actually int, it should be string? Commented Nov 29, 2016 at 19:05
  • integer value i.gyazo.com/0dbc7c3033b7cc6c2929bd6e0379464d.png @Valter Commented Nov 29, 2016 at 19:05
  • Do you want it to bind based off of the id or the name of the selected value? Commented Nov 29, 2016 at 19:10

1 Answer 1

1

When selecting options from a dropdown, type matters for binding purposes. When using ng-options, you can use as to bind something to the model as a non-string value. In your case, you may want to bind to the integer value of the id.

Syntax: select as label for value in array

> select: The value that gets bound to ng-model
> label: What value visibly shows up in the dropdown
> value: Current item in array
> array: Data source for generating the options

Example of binding to an integer value:

var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope) {
  $scope.props = [{Grade: 1}];
  $scope.propGradings = [{ name: "1", id: 1 }, { name: "2", id: 2 }, { name: "3", id: 3 }, { name: "4", id: 4 }];

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  Binding with an integer value:
  <div ng-repeat="prop in props" style="margin-bottom: 10px;">
    <label class="col-md-4 control-label">Property {{$index + 1}} ({{prop.AddressLine1}})</label>
    <div class="col-md-8">
      <select ng-model="prop.Grade" class="form-control" ng-options="opt.id as opt.name for opt in propGradings">
        <option ng-selected="{{option.id == prop.Grade}}" ng-repeat="option in propGradings" ng-value="{{option.id}}">{{option.name}}</option>
      </select>
    </div>
  </div>
</div>

Example of binding to a string value:

var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope) {
  $scope.props = [{Grade: "1"}];
  $scope.propGradings = [{ name: "1", id: 1 }, { name: "2", id: 2 }, { name: "3", id: 3 }, { name: "4", id: 4 }];

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  Binding with a string value:
  <div ng-repeat="prop in props" style="margin-bottom: 10px;">
    <label class="col-md-4 control-label">Property {{$index + 1}} ({{prop.AddressLine1}})</label>
    <div class="col-md-8">
      <select ng-model="prop.Grade" class="form-control" ng-options="opt.name as opt.name for opt in propGradings">
        <option ng-selected="{{option.id == prop.Grade}}" ng-repeat="option in propGradings" ng-value="{{option.id}}">{{option.name}}</option>
      </select>
    </div>
  </div>
</div>

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.