json - AngularJS: display different value in select menu on select? -
i have menu displays states, user select. in menu pops up, want display 2-letter state code along state name, once state selected user want display 2-letter code.
my menu is
<select ng-model="ctrl.user.state" name="state" ng-options="option.code + ' - '+ option.name option in ctrl.state.options | orderby: 'name' track option.code" class="form-control" required></select>
which displays states in format ct - connecticut
given json data, structured (partial):
{ "name": "connecticut", "code": "ct" },
i hope close want. used 1 more property on options array, , change text on ng-change.
http://jsfiddle.net/kn9xx/1433/
html
<div ng-app="myapp"> <div ng-controller="firstctrl"> <select ng-model="selectedvalue" name="state" ng-options="option.code option.code + option.nametemplate option in options" ng-change="changetext(selectedvalue)" class="form-control" required></select> </div> </div>
js
var myapp = angular.module('myapp', []); myapp.controller('firstctrl', function ($scope) { $scope.options = [ { "name": "connecticut", "nametemplate": "- connecticute", "code": "ct" }, { "name": "connecticut", "nametemplate": "- connecticute", "code": "ab" } ]; $scope.selectedvalue = ""; $scope.changetext = function(selectedvalue) { for(var = 0, m = $scope.options.length; < m; i++) { if($scope.options[i].code === selectedvalue) { $scope.options[i].nametemplate = ""; }else { $scope.options[i].nametemplate = " - " + $scope.options[i].name; } } } });
there improve on code, idea change text in ng-options. let me know if have question :)
Comments
Post a Comment