Himmators
Himmators

Reputation: 15006

How to get parent node in angular

I loop an array inside another array in angular.

fields:
            [
                {
                    label: 'First Name',
                    name: 'firstname',
                    key: '',
                    type: 'text',
                    //fixa requierd i templatesen
                    required: true
                },
                {
                    label: 'Last Name',
                    name: 'lastname',
                    key: '',
                    required: true,
                },
                {
                    label: 'Email',
                    name: 'email',
                    key: '',
                    required: true,
                    type: 'email',
                },
                {
                    key: 'test',
                    type: 'radio',
                    labels:
                    [
                        {
                            name: 'media',
                            label: 'Media'
                        },
                        {
                            name: 'frilans',
                            label: 'Frilans'

                        }

                    ],
                }
            ],

Whilst looping through field.labels, I want to access it's sibling "key". I tried using $parent scope, but that will include the entire scope. Then angular doesn't know what key it should use.

Upvotes: 0

Views: 1640

Answers (1)

AlwaysALearner
AlwaysALearner

Reputation: 43947

<div ng-repeat="field in fields">       
    <div ng-repeat="subfield in field.labels">
        <!-- gets you parent's key -->
        {{field['key']}}
        <!-- you can play around with $index to reach the parent's siblings -->
        <!-- just make sure index is in the range though -->
        {{fields[$parent.$index - 1]['key']}}
        {{fields[$parent.$index + 1]['key']}}
    </div>
</div>

Upvotes: 1

Related Questions