Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

Similar questions have bee asked before and I have tried solutions given to them, but my problem is little different.

Lets say I have an array of objects like this in my controller

$scope.objArray = [
  {
    id:1,
    name:"placeholder1"
  },
  {
    id:2,
    name:"placeholder2"
  }
];

now based on this array I am generating a form dynamically like this

  <div ng-repeat="field in fields">
    {{field.field_name}} </br>
    <input type="text" ng-model="field.field_name"><br>
  </div>

here in this form i want ng-model value to be set as placeholder1 and placeholder2 and not like javascript variable like field.field_name because my targets are coming from another source which contains html like below.

<p>this is sample html {{placeholder1}}. again sample html {{placeholder2}} </p>

i don't have those JS variables in here. I could not find any solution which will work this way for me so far.

here is link to plunk: http://plnkr.co/edit/ttaq0l3PDRu4piwSluUX?p=preview

share|improve this question
2  
Please provide a minimal working example either here (using the code snippet functionality) or on jsFiddle, Plunker, etc. It's not really clear where your variables are coming from, so your question is impossible to answer as it stands right now. – Agop Jul 8 at 18:40
    
@Agop added link to plunk – Abhishek Bansal Jul 8 at 18:46
    
I dont really understand what you're asking... – tymeJV Jul 8 at 18:48

1 Answer 1

up vote 2 down vote accepted

In your case it will look like this:

<div ng-repeat="field in fields">
    {{field.field_name}}
    <br>
    <input type="text" ng-model="$parent[field.field_name]">
    <br>
</div>

Note, that you have to use $parent reference because in your case you want to set property of the outer scope (where objArray is defined), however ngRepeat creates child scopes per iteration, so you need one step up.

Demo: http://plnkr.co/edit/35hEihmxTdUs6IofHJHn?p=info

share|improve this answer
    
and i wasted a whole day ! thank you very much !!! – Abhishek Bansal Jul 8 at 18:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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