I have a select dropdown and I am populating it with an array of objects. I am also assigning a default value to it so that a specific option is selected by default. When I change the option to something else, the option value changes (you can see this in the pre tag), but the option label/text appears blank. If I try to select some other option now, it selects the option & also populates it's label/text correctly.
ISSUE:
So after the page loads, when I change the select option to a different value, the label/text does not populate. How to fix this so that the appropriate label shows up correctly?
Here is all my code:
angular.module('myApp', [])
.controller('TodoCtrl', TodoCtrl);
function TodoCtrl($scope, $window) {
$scope.fieldObj = {};
$scope.allMembers = [{
"member_id": "1",
"firstname": "a"
}, {
"member_id": "2",
"firstname": "b"
}, {
"member_id": "3",
"firstname": "c"
}, {
"member_id": "4",
"firstname": "d"
}, {
"member_id": "5",
"firstname": "e"
}, {
"member_id": "6",
"firstname": "f"
}];
$scope.allMembers.unshift({
member_id: "new",
firstname: "Add New"
});
$scope.fieldObj.firstName = {};
$scope.fieldObj.firstName = {
"member_id": "3",
"firstname": "c"
};
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-rc.2/angular.js" rel="script"></script>
<div ng-app="myApp" class="container">
<h2>Todo</h2>
<div ng-controller="TodoCtrl">
<select ng-model="fieldObj.firstName" ng-options="member.member_id as member.firstname for member in allMembers track by member.member_id">
<option value=""></option>
</select>
<pre>
{{fieldObj.firstName | json}}
{{message}}
</pre>
<div ng-if="fieldObj.firstName === 'new'">New option was selected</div>
</div>
</div>