Trying to use the latest version (1.5.8) of AngularJS and ng-options to populate a dropdownlist.
Issue is that it's adding the data type as well as the value, like so:
<select class="possedetail ng-valid ng-dirty ng-valid-parse ng-touched" ng-model="Province" ng-options="p as p for p in provList">
<option value="string:ALBERTA" label="ALBERTA">ALBERTA</option>
<option value="string:BRITISH COLUMBIA" label="BRITISH COLUMBIA">BRITISH COLUMBIA</option></select>
I need string:Alberta'...
This is my data source:
$scope.provList = ["ALBERTA","BRITISH COLUMBIA","MANITOBA","NEW BRUNSWICK","NEWFOUNDLAND AND LABRADOR","NORTHWEST TERRITORIES","NOVA SCOTIA","NUNAVUT","ONTARIO","PRINCE EDWARD ISLAND","QUEBEC","SASKATCHEWAN","YUKON",];
I have read the google documentation, searched the web and tried changing my data source format to [{name: "Alberta"}, {name:"BC"}]...
Please help, any way around this?
Trying to use the latest version (1.5.8) of AngularJS and ng-options to populate a dropdownlist.
Issue is that it's adding the data type as well as the value, like so:
<select class="possedetail ng-valid ng-dirty ng-valid-parse ng-touched" ng-model="Province" ng-options="p as p for p in provList">
<option value="string:ALBERTA" label="ALBERTA">ALBERTA</option>
<option value="string:BRITISH COLUMBIA" label="BRITISH COLUMBIA">BRITISH COLUMBIA</option></select>
I need string:Alberta'...
This is my data source:
$scope.provList = ["ALBERTA","BRITISH COLUMBIA","MANITOBA","NEW BRUNSWICK","NEWFOUNDLAND AND LABRADOR","NORTHWEST TERRITORIES","NOVA SCOTIA","NUNAVUT","ONTARIO","PRINCE EDWARD ISLAND","QUEBEC","SASKATCHEWAN","YUKON",];
I have read the google documentation, searched the web and tried changing my data source format to [{name: "Alberta"}, {name:"BC"}]...
Please help, any way around this?
Share Improve this question edited May 1, 2017 at 20:36 OverMars asked Jul 22, 2015 at 22:27 OverMarsOverMars 1,0493 gold badges17 silver badges34 bronze badges 11 | Show 6 more comments3 Answers
Reset to default 19This might be a little late, but adding "track by" to your ng-options expression should solve your problem. i.e.
<select ng-model="Province" ng-options="p for p in provList track by p"></select>
If you are properly designing your Angular app, you should not care how <option>
elements are generated and what value
attribute is assigned. This is the View. The ViewModel is what is set on the scope variable that you assigned to it.
In other words, your example works. $scope.Province
will equal the selected province string, e.g. "ALBERTA"
. Angular does the translation for you.
Then, you could submit it to your server, or do whatever you need with it:
$http.post("/some/api", {data: $scope.Province})
But, if you must, you could generate the <option>
s with ng-repeat
. It would be less efficient and more verbose, but it would work:
<select ng-model="Province">
<option ng-repeat="p in provList" value="{{p}}">{{p}}</option>
</select>
Can't tell much with out more code, can you do a fiddle. This works fine http://jsfiddle.net/zm0eq6xf/
<div ng-app="myapp">
<fieldset ng-controller="FirstCtrl">
<select
ng-options="p for p in provList"
ng-model="p"></select>
{{ p }}
</fieldset>
value
attribute of generated options? Don't. You shouldn't care about that. The only thing you ought to care about is the model value - i.e.$scope.Province
– New Dev Commented Jul 22, 2015 at 22:41