AngularJS绑定到动态模型名称(AngularJS bind to dynamic model name)

我有一个像这样的通用JSON结构

{ "parameters": [ { "key":"fieldName","posibleValues":["Val1,"Val2"], "key":"anotherField","posibleValues":["ValA,"ValB"] } ] }

不,我想做这样的事情:

<div ng-repeat="parameter in parameters"> <ng-form name="paramForm"> {{parameter.key}}: <select ng-model="request.parameter.{parameter.key}" ng-option="..." /> </ng-form> </div>

问题来自于

select ng-model="request.parameter.{parameter.key}"

所以我想设置一个名为“request.parameter.fieldName”的模型(根据JSON结构中的“key”)。

这可能吗? 或者我是否必须帮助自己进行改变?

谢谢

I have a generic JSON structure like this

{ "parameters": [ { "key":"fieldName","posibleValues":["Val1,"Val2"], "key":"anotherField","posibleValues":["ValA,"ValB"] } ] }

No I want to do something like this:

<div ng-repeat="parameter in parameters"> <ng-form name="paramForm"> {{parameter.key}}: <select ng-model="request.parameter.{parameter.key}" ng-option="..." /> </ng-form> </div>

The problems comes with the

select ng-model="request.parameter.{parameter.key}"

So I want to setup a model that is named like "request.parameter.fieldName" (according to the "key" in the JSON structure.

Is this possible? Or do I have to help myself with a ng-change?

Thanks

最满意答案

如果有人以这种方式查找任何内容,您可以使用以下语法:

request.parameter[parameter.key]

If someone is looking for anything in this way, you can use the following syntax:

request.parameter[parameter.key]

更多推荐