简体   繁体   中英

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/

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

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.

One way to do it is to simply add the necessary references to the scope, like this:

$scope.fieldF1 = fieldValue('F1');
$scope.fieldF2 = fieldValue('F2');

And then use those references:

<input ng-model="fieldF1.value"/>
<input ng-model="fieldF2.value"/>

Fiddle: http://jsfiddle.net/cbnAU/5/

Note: I'm assuming that $scope.data is static, but if it happens to be dynamic you can always watch for changes on it and recalculate the references...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM