Creating a new directive with AngularJS using HTML5 audio element

angularjs, audio, html, javascript

Solution

Audio element has source element. So you must change this element. Look here

Problem

I am new with angular and I've made audio buttons using angular directive: ``` app.directive('soundButton', [function () { return { restrict: 'E', link: function (scope, element, attrs) { var audioSrc = attrs.origem; var audio = new Audio(audioSrc); scope.play = function () { if (audio.paused) { audio.play(); } else { audio.pause(); } }; element.css({ borderRadius: '50%', width: '100px', height: '100px', backgroundColor: 'red', display: 'inline-block' }); } } }]); ``` ...and put the html code on my page: ``` <section id="bla" data-ng-controller="Ctrl"> <sound-button data-ng-click="play()" origem="http://sampleswap.org/samples-ghost/DRUMS%20(FULL%20KITS)/sys100%20drumkit/24[kb]909-klick.aif.mp3" class="col-md-4"></sound-button> <sound-button data-ng-click="play()" origem="http://sampleswap.org/samples-ghost/DRUMS%20(FULL%20KITS)/sys100%20drumkit/2[kb]hihat_closed.aif.mp3" class="col-md-4"></sound-button> <sound-button data-ng-click="play()" origem="http://sampleswap.org/samples-ghost/DRUMS%20(FULL%20KITS)/sys100%20drumkit/34[kb]noicybell.aif.mp3" class="col-md-4"></sound-button> </section> ``` But always I click the audio button, it plays the same sound. (I suspect this could be an array of elements issue) FIDDLERJS: http://jsfiddle.net/qumjmz9y/

Original source