[英]Setting default value for <select/> using ng-options with (key, value) in angularjs
我想为以下选择设置默认值:
<select ng-model="selected"
ng-options="key as value.Name for (key, value) in attributes">
</select>
我将attributes
用作映射,将id作为键,将对象作为值。 我想在选择框中显示值的名称,但将键绑定到模型,因为当我访问selected
,我需要id而不是名称。 这可行,但是现在我想设置一个默认值。 默认值还必须显示value.Name
,但必须绑定id
。
根据这个 ,它应该工作是这样的:
$scope.selected = { 1: {Name : "Attribute1"}};
我还没做到 我将不胜感激任何帮助。
编辑:
我的attribute变量如下所示(TypeScript):
export interface IAttributes {
[id: number]: IAttributeProperties;
}
export interface IAttributeProperties {
Name: string;
DataType: string;
}
您可以使用ng-init
对其进行ng-init
:
<select
ng-init="selected = attributes[0]"
ng-model="selected"
ng-options="key as value.Name for (key, value) in attributes">
</select>
解决方案是设置以下默认值:
$scope.selected = '1';
所以我只需要设置id
(密钥)。 现在在选择框中显示“ Attribute1”,并且绑定了相关的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.