JakeP
JakeP

Reputation: 327

How can I write a button in an ng-repeat that affects the object the element is based on?

I have a bunch of recordings in an array. Each recording is an object with a lot of different components.

I have an ng-repeat that for each of those records, shows a box with the recording's name, length, and then a play and delete button.

I know how to write a click event for when the button is pressed, but how do I know which object in the array the button belonged to? The play and delete buttons need to act on that record and that record alone.

Upvotes: 0

Views: 232

Answers (2)

Dylan
Dylan

Reputation: 4773

Just another way by index :

$scope.recordings = [];
$scope.play = function($index){
   $scope.playing = $index;
   $scope.item = $scope.recordings[$index];
}
...   
ng-repeat="rec in recordings" ng-click="play($index)"></div>

Upvotes: 0

Ben Jaspers
Ben Jaspers

Reputation: 1546

You can either pass the object to the ng-click function:

<div ng-repeat="record in records">
    <button ng-click="play(record)">Play</button>
</div>

or call a function on the record in the ng-click:

<div ng-repeat="record in records">
    <button ng-click="record.play()">Play</button>
</div>

Upvotes: 3

Related Questions