I want to use the HTML5 audio element attributes (play/stop/pause/...) in a controller in Angular. But I have multiple audio elements generated by a foreach loop so selecting by ID is not an option (except if I give id's based on the index of my foreach, which doesn't seem the right way).
My abbreviated html:
<div ng-controller="LoopController">
<a ng-click="playLoop()">Play</a>
<audio id="music" controls loop>
<source src="songs/music1.mp3" type="audio/mpeg">
</audio>
</div>
<div ng-controller="LoopController">
<a ng-click="playLoop()">Play</a>
<audio id="music" controls loop>
<source src="songs/music2.mp3" type="audio/mpeg">
</audio>
</div>
Angular controller:
gloopsApp.controller('LoopController', ['$scope', function($scope) {
var music = document.getElementById('music');
//here I want to target the audio element that is in this controller
$scope.playLoop = function() {
if (music.paused) {
music.play();
} else {
music.pause();
}
};
}]);