AngularJS drop down (ng- options) not binding - string to object (initial selection) -
AngularJS drop down (ng- options) not binding - string to object (initial selection) -
i having problem binding info retrieved server drop downwards list. main issue think fact comparing done on differing object types.
for example: 1. object returned server contains currency code string. want bound item in dropdown list
"basecurrencycode":"gbp"
the view model returns list of currencies.. these returned list of currency objects different properties
{"currencies":[{"id":1,"rateid":0,"abbreviation":"afn","description":"afghani","rate":0.0,"ratedescription":null,"languagecode":"en-gb","isdefault":true,"fulldescription":"afn - afghani - ","shortdescription":"afn - afghani"}}
etc.
currently, have got working writing function go through every property every item in list, find right property wish compare - comparing , homecoming initial selection.
when calling save method need manually bind currency abbreviation object wish homecoming server.
surely there must improve way this?
some of code reference..
<select ng-model="selectedcurrency" ng-options="currency.shortdescription currency in viewmodel.currencies"></select> // phone call custom method..list, propertyname, value compare $scope.selectedcurrency = initialisedropdown($scope.viewmodel.currencies, "abbreviation", $scope.updatedobject.basecurrencycode); // code executed when saving - bind currency updated object $scope.updatedobject.basecurrencycode = $scope.selectedcurrency.abbreviation;
any help appreciated!
edit sorry if wasn't clear enough.. summarise..
the main problem here binding drop downwards , initial selection.
the object updating contains parameter (string) of currency abbreviation.
the list select list of currency objects. these 2 differing object types have been unable plug in angulars 2 way binding , have written code on initial retrieval , when saving.
the cleanest way prepare homecoming currency object in our retrieval instead of simple string of abbreviation, not option.
is there improve way of enabling 2 way binding on different object types ?
thanks again
it not clear problem is, can save work binding <select>
selected currency object (so don't have later). select
+ ngoptions
allow bind 1 value while displaying another, next syntax:
<select ng-model="selectedcurrency" ng-options="currency currency.shortdescription currency in viewmodel.currencies"> </select>
in above example, $scope.selectedcurrency
bound whole currency
object, currency.shortdescription
displayed in dropdown.
see, also, short demo.
update:
in case don't need bind whole currency object, bind updatedobject
's basecurrencycode
property abbreviation of selected (in dropdown) currency, can this:
<!-- in view --> <select ng-model="updatedobject.basecurrencycode" ng-options="c.abbreviation c.shortdescription c in currencies"> </select> // in controller $scope.currencies = [...]; $scope.updatedobject = { ... basecurrencycode: <basecurrencycodefromserver> };
see, also, short demo.
angularjs ng-options
Comments
Post a Comment