简体   繁体   English

我可以使用ng-model setter-getter选项转换模型中变量的值吗?

[英]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 : 我在angularjsangularjs以下情况:

<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 . 我想设置$ scope.value,而不是与价值type.description ,但其对应的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 ? 我可以使用ng-model的getterSetter选项完成此操作吗? I have looked the documentation and it does not clarify much. 我看了文档,但并不清楚。

You don't need getterSetter to do that. 您不需要getterSetter来执行此操作。 All you need is ng-options : 您需要的只是ng-options

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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