Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am fairly new to Angular JS and I have run into a problem. I am creating a dynamic form where the elements come from the Database for example: order object:

public int Id { get; set; }
public string LenderName { get; set; }   
public PropertyModel Property { get; set; } 

PropertyModel Object:

public string Zip{ get; set; }
public string State { get; set; }

I was successfully able to dynamically bind ng-model for single level deep property like Example for Order.LenderName my input element is:

<input type="text" ng-model="order[element.ModelName]"></input>

this perfectly binds to the $scope.order.LenderName

Note: element.ModelName = "LenderName" or rather I could say the order property name

my sample code is below:

<div ng-repeat="element in eventSchemaElementList">
    <div ng-switch="element.FieldTypeCode">
        <div class="topBottomPadding">
            <div class="col-sm-2 alignRight">
                <label>{{element.SchemaFieldName}}:</label>
            </div>
            <div ng-switch-when="TextBox" class="col-sm-10">
                <input type="text" ng-model="order[element.ModelName]" />
            </div>
            <div ng-switch-when="TextArea" class="col-sm-10">
                <textarea ng-model="order[element.ModelName]"></textarea>
            </div>
        </div>
    </div>
</div>

In the above code I am not able to figure out How I can dynamically bind Order.Property.State to ng-model. the above code works only for one level deep. I am not sure how to do it for order.Property.State I also tried doing this:

<input type="text" ng-model="order[element.ModelName]" />

Where element.ModelName = Property[State]

I also tried hardcoding it as order[Property[State]] in the input still did not work

<input type="text" ng-model="order[Property[State]]" />

For the above ones It does not bind to the order.Property.State but it creates a new property as Property[State]. It is really confusing because I have objects that go 4 or 5 level deep.

Help or suggestions will be really appreciated thanks in Advance !!!

share|improve this question

I don't know C# so I'm not exactly sure what your model (data) looks like. Could you give us some example data in JSON so that we can run against your view?

I don't know if it's actually not allowed is JavaScript, but I don't write order[Property[State]] but use dot-notation order.Property.State. Hope that helps.

share|improve this answer
    
Thanks for the reply. It actually works with order.Property.State but it should be hardcoded thats the issue and I am trying to bind to ng-model dynamically. – user3195241 Jan 14 '14 at 20:41
    
below is the example data in JSON Documents: Array[0] Id: 0 LenderLoanNumber: "123124234" LenderName: "ClientABC" Product: "Something" Property: Object PropertyType: null State: null – user3195241 Jan 14 '14 at 20:49
    
Sorry I don't really understand what you mean. You say it "should be hardcoded" but you want to "bind dynamically" that's kind of an oxymoron. Also, if you have the time, a JSFiddle would be great. – NicolasMoise Jan 14 '14 at 21:15
    
I mean when I use it like <input type="text" ng-model="order.Property.State" /> it works but I want to bind it dynamically using the object without having to say order.Property.State. I could provide a sample JSFiddle that would be better I guess .thanks alot for the reply. – user3195241 Jan 14 '14 at 21:26

Try creating the string order['Property']['State'] in controller e.g. $scope.myString = 'order[\'Property\'][\'State\']' and refer the same in ng-model as ng-model="a = myString".

share|improve this answer

if u want to have order.Property.State

in json or array should be

name = property
and subname = state

which u can call order[element.name][element.subname]

share|improve this answer

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.