我正在使用angularjs制作一个示例播放器,利用元素指令。我希望在指令的模板内部保留事件,使其仅限于指令内部。换句话说,我如何使用指令中的控制器来创建仅限于指令模板内部元素的事件。
指令:
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>'
};
});
我唯一能让 ng-click 事件正常工作的方法是将事件方法放置在父控制器的 $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();
};
我该如何在指令控制器中实现play()
和stop()
事件的包含?
$scope.play = function() {};
时,没有任何反应。http://paste.ofcode.org/MBuDatxyYf2tDXp8BhgLcq - cj5