I've seen a few questions here similar to this. In fact that's how I got this far. But one little piece isn't working. I have the following:
<select id="facility" class="form-control"
ng-model="MainObj.facility" name="facility"
ng-options="fclty.value as fclty.name for fclty in MainData.fclts">
<option value=""></option>
</select>
I've checked via logging that MainData.fclts
contains exactly what I want, but each option outputs as
<option value="0" label="Actual Facility Name">Actual Facility Name</option>
<option value="1" label="Actual Facility Name">Actual Facility Name</option>
(etc)
In other words, it's putting the name
in there correctly, but just using a 0, 1, 2, ...
for the value instead of what's in the value field.
What am I missing here?
Thanks.
EDIT: Sample data:
[value: '00001', name: "John's House"]
[value: '00002', name: "School"]
[value: 'testval', name: "Testing Building"]
(etc)
EDIT 2:
Just for the fun of it, I swapped the two and did fclty.name as fclty.value
. Sure enough the value
showed up in the dropdown but the value portion of the option
was still the default 0, 1, 2, ..
So, it can SEE that field if it wants to, but, for some reason, doesn't want to use it to fill in the value of the option
.
What does your MainObj and MainData look like? This example works exactly as it should:
angular.module("myApp",[])
.controller("main",["$scope", function($scope){
$scope.MainData = {
fclts: [
{name:"Place", value: "00001"},
{name:"Other Place", value: "00002"},
{name:"Thrid Place", value: "testval"}
]
};
$scope.MainObj = {facility: "00002"};
}]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<body ng-app="myApp" ng-controller="main">
<select ng-model="MainObj.facility" name="facility" ng-options="fclty.value as fclty.name for fclty in MainData.fclts">
</select>
{{MainObj.facility}}
</body>
Your sample data is not in valid syntax, please confirm that they are formatted like objects:
[
{value: "00001", name: "John's House"},
{value: "00002", name: "School"},
{name:"Thrid Place", value: "testval"}
]