Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Binding to a specific item of an array in Angular

Given a data structure that contains an array of JavaScript objects, how can I bind a certain entry from that array to an input field using Angular?

The data structure looks like this:

$scope.data = {
    name: 'Foo Bar',
    fields: [
        {field: "F1", value: "1F"},
        {field: "F2", value: "2F"},
        {field: "F3", value: "3F"}
    ]
};

The fields array contains several instances of the given structure, with each entry having both a field attribute and a value attribute.

How can I bind an input control to the value field attribute of the array entry with the field F1?

<input ng-model="???"/>

I know that I could bind all fields using an ng-repeat, but that's not what I want. The above data is just an example from a much larger list of fields, where I only want to bind a pre-defined subset of fields to controls on the screen. The subset is not based on the attributes in the array entries, but is known at design time of the page.

So for the above example, I would try to bind F1 to one input on the page, and F2 to another one. F3 would not be bound to a control.

I've seen examples where a function was used in the ng-model, but it doesn't seem to work with Angular 1.1.0.

Is there another clever way to bind the input field to a specific array entry?

Here's a fiddle that has an example, but does not work since it's trying to use function in the ng-model attribute: http://jsfiddle.net/nwinkler/cbnAU/4/

Update

Based on the recommendation below, this is what it should look like: http://jsfiddle.net/nwinkler/cbnAU/7/

like image 364
nwinkler Avatar asked Dec 14 '12 14:12

nwinkler


People also ask

How to bind data in Angular 2?

The data binding in Angular can be broadly classified into two groups. One way binding or two-way binding In one way binding data flows from one direction. Either from view to component or from component to view. To bind data from component to view, we make use of Interpolation & Property Binding.

How to bind data in an angular web application using curly braces?

Along with that, you will learn how to bind data in an Angular web application using curly braces to show on the frontend. Data Binding has been a part of AngularJS since Angular. In the code, you go for curly braces to denote data binding – { { variable goes here }} and this process is referred to as interpolation.

What is the use of nG-model in angular?

The ng-model directive provides a two-way binding between the model and the view. Two-way Binding. Data binding in AngularJS is the synchronization between the model and the view. When data in the model changes, the view reflects the change, and when data in the view changes, the model is updated as well.

Are there any exceptions to the angular data binding rule?

Exceptions to this are very rare. One last time: Angular binds component data to properties, not attributes! Referring back to the example, the [ … ] in the element’s property assignment have special meaning. The brackets show that property is bound to “value” on the right of the assignment.


2 Answers

I personally would reorganize the array in a way that field property of an entry of the array become the identifier of the object. Mhhh that sentence may sound strange. What I mean is the following:

$scope.data = {
    name: 'F1',
    fields: {
        F1: {
           value: "1F"
        },
        F2: {
           value: "2F"
        }
    }
};

If you want to bind a the value dynamically and it's an easy and quick way to achieve it. Here is your fiddle modified so that it words. http://jsfiddle.net/RZFm6/

I hope that helps

like image 91
F Lekschas Avatar answered Sep 28 '22 09:09

F Lekschas


You can use an array of objects, just not an array of strings.

HTML:

<div ng-repeat="field in data.fields">
    <input ng-model="field.val"/>
</div>

JS:

$scope.data = {
    name: 'F1',
    fields: [
        { val: "v1" },
        { val: "v2" }
    ]
};

I've updated @Flek's fiddle here: http://jsfiddle.net/RZFm6/6/

Edit: Sorry just read your question properly, you can still use an array with:

<label>Bound to F1:</label>
<input ng-model="data.fields[0].value"/>

though maybe stop and think. Is there going to be variable number of fields ? or are you making a predetermined number of fields ? Use an array in the former and an object for the latter.

like image 41
jpillora Avatar answered Sep 28 '22 10:09

jpillora