如何在影响元素所基于的对象的 ng-repeat 中编写按钮?
How can I write a button in an ng-repeat that affects the object the element is based on?
我在数组中有一堆录音。每个录音都是一个包含很多不同组件的对象。
我有一个 ng-repeat
,对于每条记录,显示一个框,其中包含记录的名称、长度,然后是播放和删除按钮。
我知道如何为按下按钮时编写点击事件,但我如何知道按钮属于数组中的哪个对象?播放和删除按钮需要作用于该记录并且仅作用于该记录。
您可以将对象传递给 ng-click 函数:
<div ng-repeat="record in records">
<button ng-click="play(record)">Play</button>
</div>
或者在ng-click中对记录调用一个函数:
<div ng-repeat="record in records">
<button ng-click="record.play()">Play</button>
</div>
索引的另一种方式:
$scope.recordings = [];
$scope.play = function($index){
$scope.playing = $index;
$scope.item = $scope.recordings[$index];
}
...
ng-repeat="rec in recordings" ng-click="play($index)"></div>
我在数组中有一堆录音。每个录音都是一个包含很多不同组件的对象。
我有一个 ng-repeat
,对于每条记录,显示一个框,其中包含记录的名称、长度,然后是播放和删除按钮。
我知道如何为按下按钮时编写点击事件,但我如何知道按钮属于数组中的哪个对象?播放和删除按钮需要作用于该记录并且仅作用于该记录。
您可以将对象传递给 ng-click 函数:
<div ng-repeat="record in records">
<button ng-click="play(record)">Play</button>
</div>
或者在ng-click中对记录调用一个函数:
<div ng-repeat="record in records">
<button ng-click="record.play()">Play</button>
</div>
索引的另一种方式:
$scope.recordings = [];
$scope.play = function($index){
$scope.playing = $index;
$scope.item = $scope.recordings[$index];
}
...
ng-repeat="rec in recordings" ng-click="play($index)"></div>