0

I have a dynamic form that receives a array of jsons with the field to be displayed. My problem is in the ng-model and ng-required they are in the array,cant get the value to be in the ng-model name

This is my code:

the form containing the data

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 to display the form fields

 <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 Answers 2

2

Replace the

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

with following

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

In JavaScript when you want to use a variable as a dictionary key, you use the [] accessor instead of dot.

Sign up to request clarification or add additional context in comments.

Comments

0

for ng-model try ng-model=requestForm[fa.name]

4 Comments

Doesnt get the variable name, ive tried that. And I need the to be ng-model=requestForm.name
@changez hm, maybe requestForm[{{ fa.name }}]
Please add some details to your answer so that both OP and future readers can benefit with the rationale behind the suggested solution
@user2954587 Sry it worked using requestForm[fa.name] instead of not appearing in the html when the info arrives into the controller it gets the fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.