Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array like var names=["price","quality","service"], I have to convert that array to object like names=[{"name":"price", "value":"1.3"},{"name":"quality","value":"3"},{"name":"service","value":"3.4"}]. Each and every time array values are different. I need to do build the input field with that values by using ng-repeat like

<div ng-repeat="n in names">
      <input name="rating" ng-model="n" value="0"/>
</div>

How I can fetch the values as objects when user submits the form. can any one help me.

Update

I need json as like this:::

names=[{"name":"price", "value":"1.3"},{"name":"quality","value":"3"},{"name":"service","value":"3.4"}]

ThanQ!

share|improve this question

closed as unclear what you're asking by entre, gunr2171, rene, TGMCians, ProgramFOX Nov 2 '14 at 18:56

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

    
where are the values coming from?? –  user2165424 Oct 28 '14 at 10:14
    
values are comming from database –  chandu Oct 28 '14 at 10:30

1 Answer 1

Declare you model object

$scope.item={};

Then just do

<div ng-repeat="n in names">
      <input name="rating" ng-model="item[n]" value="0"/>
</div>

The data will be collected into item object.

Update: To get such in such a model, we would have to create it before binding it to view. Assuming names has all the items in the controller do:

var items=names.map(function(name){ return { name:name,value:null}; })

This creates an array of the format you require.

Now bind the view to:

<div ng-repeat="item in items">
          <input name="rating" ng-model="item.value" value="0"/>
    </div>
share|improve this answer
    
Hi @Chandermani, I have updated the question. Could you Please help to solve that one –  chandu Oct 29 '14 at 4:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.