0

I have a list of names that is connected to different objects in an array. I would like to, whenever you click one of the names, get the values and output them in another list respective to that that object. For right now I console.log the right values, but don't know how to output it:

<a href="#" id="name" ng-click="updateIndex($index);">{{ person.name }}</a>

Controller:

$scope.updateIndex = function(index) {
            console.log($scope.person[index]); //Logs the right values
        }

How can I output the values from the object which name is clicked to:

<div class="wrap" ng-repeat="per in person">

        <div class="box">
            {{ person.name }}
        </div>

        <div class="box">
           {{ person.age }}
        </div>

        <div class="box">
           {{ person.town }}
        </div>

        <div class="box">
           {{ person.country }}
        </div>

        <div class="box">
           {{ person.gender }}
        </div>

</div>

Thanks!

1
  • what do you mean by output the values from the object? Commented Aug 6, 2016 at 9:28

2 Answers 2

1

You can store the variable in the variable an use it:

$scope.updateIndex = function(index) {
     $scope.currentPerson = $scope.person[index] 
}

<div class="wrap" ng-repeat="per in currentPerson">

    <div class="box">
        {{ person.name }}
    </div>

    <div class="box">
       {{ person.age }}
    </div>

    <div class="box">
       {{ person.town }}
    </div>

    <div class="box">
       {{ person.country }}
    </div>

    <div class="box">
       {{ person.gender }}
    </div>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfect. Thanks!
0

Just assign $scope.person[index] to another variable in the click function and output it in the template.

1 Comment

Glad to hear that. Please, mark one of the answers as the correct one to let everyone know, that this topic is solved. Thanks. Also +1 is also a nice way to thank anybody who helped you get to the correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.