简体   繁体   中英

Can I use ng-model setter-getter option to transform the value of a variable in the model?

I have the following situation in angularjs :

<select ng-model="value">
    <option ng-repeat="type in types">{{type.description}}</option>
<select>

The controller looks like:

$scope.value = null;
$scope.types=[{description: "asdf", id:"a"}, {description: "basdf", id:"b"}];

I want to set $scope.value, not with the value of type.description , but with the value of its corresponding id: type.id . But I still want to show the description in the dropdown select.

Can I accomplish this using the getterSetter option of ng-model ? I have looked the documentation and it does not clarify much.

You don't need getterSetter to do that. All you need is ng-options :

<select ng-model="value" ng-options="type.id as type.description for type in types">
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM