gpt4 book ai didi

javascript - HTML 和 Angular 中的动态变量?

转载 作者:行者123 更新时间:2023-11-28 15:10:23 25 4
gpt4 key购买 nike

我通过包含显示名称和变量的对象列表使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com