我通过包含显示名称和变量的对象列表使用ng-repeat
。例如,下面是一个示例字段列表:
"fields": [
{"displayName": "Company Name", "variable": "name"},
{"displayName": "Location of Product", "variable": "location"},
]
目前我正在做这样的事情:
<div ng-repeat="field in fields">
<label class="control-label">{{field.displayName}}</label>
model.{{field.variable}}
</div>
我希望 model.{{field.variable}}
显示 model.variable
值的值。例如,如果 field.displayName
是“公司名称”,那么我想显示 model.name
。
我尝试将其全部包裹在大括号中 {{model.{{field.variable}}}}
但没有成功。
谢谢!
最佳答案
当您有动态 key 时,需要使用括号表示法。
<div ng-repeat="field in fields">
<label class="control-label">{{ field.displayName }}</label>
{{ model[field.variable] }}
</div>
关于javascript - HTML 和 Angular 中的动态变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36726398/