我不知道我对这个概念的理解是否错误,但是几个小时的搜索并没有产生任何结果。
我想根据用户从下拉列表中选择的“类型”动态加载表单的字段。这通过 Angular 绑定(bind)到变量“currentType”。该部分有效。
currentType = //whatever the user chose. Will evaluate to either "category1" or "category2"
currentFields = {}
fields = {
'category1': {
propertyInfo {
'label1'
'label2'
'label3'
'label4'
'category2': {
propertyInfo {
'label5'
'label6'
'label7'
'label8'
我想要发生的是,当选择类型时,将加载字段。所以:
currentFields = field."currentType".propertyInfo
我不知道如何将该变量插入到对象路径中。我不断收到有关字段没有属性“currentType”的错误。
最佳答案
应该像这样简单(感谢 JS 的动态特性):
currentFields = field[currentType].propertyInfo
如果您不知道,它的工作原理如下。如果你有一个对象:
var foo = {
bar: "42"
}
值“42”
可以通过foo.bar
或foo['bar']
访问。
关于javascript - 在 JavaScript 和 Angular 中动态地将变量插入到对象路径中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29608723/