I am working on a sample player using angularjs, utilizing an element directive. I would like the events within the directive's template to remain contained within the directive. In other words, how can I use the controller in the directive to create events scoped only to elements within the directive's template.
The directive:
logsApp.directive('logPlayer', function() {
return {
restrict: 'E',
replace: true,
scope: {audio: '='},
template: '<div ng-repeat="sample in audio">' +
'{{sample.title}}' +
'<button type="button" class="btn btn-default" ng-click="play()">Play</button>' +
'<button type="button" class="btn btn-danger" ng-click="stop()">Stop</button>' +
'</div>'
};
});
The only way I can get the ng-click events to work is place the event methods in the parent controller's $scope
:
logsApp.controller('LogListController', ['$scope', '$rootScope', 'LogsService', 'SoundManager', function($scope, $rootScope, LogsService, SoundManager) {
...
$scope.play = function() {
SoundManager.sound.createSound({
id: SoundManager.idPrefix + this.sample.id,
url: this.sample.filename,
autoLoad: true,
autoPlay: false,
onload: function() {
this.setPosition(0);
this.play({
whileplaying: function() {
...
}
});
}
});
};
$scope.stop = function() {
SoundManager.sound.stopAll();
};
How would I accomplish getting the play()
and stop()
events to be contained within the directive controller?