0

I have these objects right here that I will use to save data from a form, and later send it to an api as JSON :

$scope.price = {} 
$scope.item = {"price":$scope.price, };

I also have these field which will be used to dynamically generate inputs on a html page:

$scope.fields = [
    {
        name: $scope.item.title,
        title: 'Title',
        type: {
            view: 'input'
        }
    },
    {
        name: $scope.price.regular,
        title: 'Regualar Price',
        type: {
            view: 'input'
        }
    }        
];

Now in order to generate the form I use this code:

<div class="form-group" ng-repeat="field in fields">
            <label>{{ field.title }}:</label>  
            <span ng-switch on="field.type.view">  
                <span ng-switch-when="input">  
                    <input  
                              ng-model=field.name
                              type="text"  
                              />  
                </span>  
            </span>
        </div>

And with this code, it is not assigning the values in the input to the objects. Is there a way to do it? I know I can do it this way:

ng-model="item[field.name]"

But that limits me to only one level of the object. I want to be able to bind nested objects. And I just can't seem to figure it out. Thank You!

2
  • there is already a directive to build forms from a JSON Schema object; see schemaform.io. Commented Jul 13, 2015 at 1:47
  • You can't simply generate fields with ng-repeat. See this post and particularly this answer : stackoverflow.com/questions/21631700/… You need to use $compile to insert your fields with a directive. Commented Jul 13, 2015 at 2:03

0

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.