带有变量的ng-model名称

时间:2015-12-04 14:30:46

标签: javascript angularjs json

我有一个动态表单,它接收带有要显示的字段的jsons数组。我的问题在ng-model和ng-required它们在数组中,不能得到值ng-model名称

这是我的代码:

包含数据的表单

form_array : [
    {label: "Responsable", name: "dynamic", required: true},
    {label: "Username", name: "user1", required: true},
    {label: "Number", name: "user2", required: false},
    {label: "Age", name: "user3", required: false}
]

html显示表单字段

 <form name="form" class="form-submit">
    <div class="form-group">
        <div ng-repeat="fa in form_array ">
        <label>{{fa.label}} {{fa.name}} {{fa.required}}</label> <input class="form-control" type="text" 
            ng-model="requestForm.{{fa.name}}" ng-required={{fa.required}}></div>
    </div>
    <button id="resetbtn" class="btn btn-primary" type="reset" value="Reset">Limpar</button>
    <button id="sendReport" class="btn btn-primary" ng-disabled="requestForm.$invalid"
            ng-click="send(form)">

2 个答案:

答案 0 :(得分:2)

替换

ng-model="requestForm.{{fa.name}}"

以下

ng-model="requestForm[fa.name]"

在JavaScript中,当您想要将变量用作字典键时,可以使用[]访问器而不是点。

答案 1 :(得分:0)

ng-model尝试ng-model=requestForm[fa.name]