Hey there, I'm currently attempting to select an item from an array in the select options by using a string value.
Below is my HTML code:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-controller="AppCtrl" ng-app="myApp">
<select ng-model="gender" ng-options="g.vl as g.lbl for g in genders track by g.vl">
</select>
</body>
</html>
Here's the JavaScript code:
var app = angular.module('myApp', []);
app.controller('AppCtrl', function($scope) {
$scope.genders = [ {lbl: 'Male', vl: 'male'}, {lbl: 'Female', vl: 'female'}];
$scope.gender = 'female';
});
It works fine without the "track by" clause and selects the initial value, but I need to include it to use the value from the database (though it currently selects an empty option). Is there a workaround for this issue without looping through the options and comparing the initial value? Thank you! Here's the snippet of the code http://jsbin.com/qixad/4/edit