JavaScript AngularJS 应用中的音频播放器插件加倍标记
JavaScript audio player plugin doubling markup in AngularJS app
我有一个 Angular 应用程序可以从数据库中提取许多音频文件。我使用 Anthony Kolber 的 audio.js 作为 HTML5 播放器,其中相关的包装器和 html 是从 javascript 生成的。问题是加价翻了一番,我不明白为什么。它在 ng-repeat 之内或之外都这样做,并在设置加载和进度条的宽度时导致连续 'undefined' 错误。
javascript 很长,很难粘贴到这里。我用代码 here as well as a live demo here.
创建了问题的简化版本
原HTML:
<!-- audio links -->
<div ng-repeat="audio in medias | orderBy: 'track'" my-repeat-directive>
<span><em>{{audio.title}}</em></span>
<audio src="" dynamic-url dynamic-url-src="{{audio.url}}" preload="auto" />
</div>
渲染的 HTM:
<div ng-repeat="audio in medias | orderBy: 'track'" my-repeat-directive="" class="ng-scope">
<span><em class="ng-binding">Glass Jungle 1</em></span>
<div class="audiojs loading" classname="audiojs" id="audiojs_wrapper0">
<div class="audiojs " classname="audiojs" id="audiojs_wrapper3">
<audio src="audio_files/Glass_Jungle_sample1.mp3" dynamic-url="" dynamic-url-src="Glass_Jungle_sample1.mp3" preload="auto"></audio>
<div class="play-pause">
<p class="play"></p>
<p class="pause"></p>
<p class="loading"></p>
<p class="error"></p>
</div>
<div class="scrubber">
<div class="progress" style="width: 0%;"></div>
<div class="loaded" style="width: 100%;"></div>
</div>
<div class="time">
<em class="played">00:00</em>/<strong class="duration">01:25</strong>
</div>
<div class="error-message"></div>
</div>
<div class="play-pause">
<p class="play"></p>
<p class="pause"></p>
<p class="loading"></p>
<p class="error"></p>
</div>
<div class="scrubber">
<div class="progress"></div>
<div class="loaded"></div>
</div>
<div class="time">
<em class="played">00:00</em>/<strong class="duration">00:00</strong>
</div>
<div class="error-message"></div>
</div>
我下载了您的代码并对其进行了一些简化,它对我有用:
<!-- audio links -->
<div ng-repeat="audio in medias | orderBy: 'track'">
<span><em>{{audio.title}}</em></span>
<audio src="" dynamic-url dynamic-url-src="{{audio.url}}" preload="auto" />
</div>
<!-- JavaScript -->
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- angularJS -->
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<!-- AngularJS Scripts -->
<script>
var app = angular.module('myApp', []);
app.controller('mainCtrl', function($scope, $rootScope) {
$scope.medias = [{
"track": 1,
"title": "Glass Jungle 1",
"url": "Glass_Jungle_sample1.mp3"
}, {
"track": 2,
"title": "Glass Jungle 2",
"url": "Glass_Jungle_sample2.mp3"
}, {
"track": 3,
"title": "Glass Jungle 3",
"url": "Glass_Jungle_sample3.mp3"
}];
audiojs.events.ready(function() {
var as = audiojs.createAll();
});
});
app.directive('dynamicUrl', function() {
return {
restrict: 'A',
link: function postLink(scope, element, attr) {
element.attr('src', 'audio_files/' + attr.dynamicUrlSrc);
}
};
});
</script>
其余同理。我没有深入研究您的代码出现问题的原因,自从我使用 angular 以来已经有一段时间了,但我认为这与 myRepeatDirective 有关,它执行了 3 次,一次对于每个音频文件。我猜标记只被复制了两次,因为第二次遇到 javascript 错误以防止它第三次执行。
我有一个 Angular 应用程序可以从数据库中提取许多音频文件。我使用 Anthony Kolber 的 audio.js 作为 HTML5 播放器,其中相关的包装器和 html 是从 javascript 生成的。问题是加价翻了一番,我不明白为什么。它在 ng-repeat 之内或之外都这样做,并在设置加载和进度条的宽度时导致连续 'undefined' 错误。
javascript 很长,很难粘贴到这里。我用代码 here as well as a live demo here.
创建了问题的简化版本原HTML:
<!-- audio links -->
<div ng-repeat="audio in medias | orderBy: 'track'" my-repeat-directive>
<span><em>{{audio.title}}</em></span>
<audio src="" dynamic-url dynamic-url-src="{{audio.url}}" preload="auto" />
</div>
渲染的 HTM:
<div ng-repeat="audio in medias | orderBy: 'track'" my-repeat-directive="" class="ng-scope">
<span><em class="ng-binding">Glass Jungle 1</em></span>
<div class="audiojs loading" classname="audiojs" id="audiojs_wrapper0">
<div class="audiojs " classname="audiojs" id="audiojs_wrapper3">
<audio src="audio_files/Glass_Jungle_sample1.mp3" dynamic-url="" dynamic-url-src="Glass_Jungle_sample1.mp3" preload="auto"></audio>
<div class="play-pause">
<p class="play"></p>
<p class="pause"></p>
<p class="loading"></p>
<p class="error"></p>
</div>
<div class="scrubber">
<div class="progress" style="width: 0%;"></div>
<div class="loaded" style="width: 100%;"></div>
</div>
<div class="time">
<em class="played">00:00</em>/<strong class="duration">01:25</strong>
</div>
<div class="error-message"></div>
</div>
<div class="play-pause">
<p class="play"></p>
<p class="pause"></p>
<p class="loading"></p>
<p class="error"></p>
</div>
<div class="scrubber">
<div class="progress"></div>
<div class="loaded"></div>
</div>
<div class="time">
<em class="played">00:00</em>/<strong class="duration">00:00</strong>
</div>
<div class="error-message"></div>
</div>
我下载了您的代码并对其进行了一些简化,它对我有用:
<!-- audio links -->
<div ng-repeat="audio in medias | orderBy: 'track'">
<span><em>{{audio.title}}</em></span>
<audio src="" dynamic-url dynamic-url-src="{{audio.url}}" preload="auto" />
</div>
<!-- JavaScript -->
<!-- jQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- angularJS -->
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<!-- AngularJS Scripts -->
<script>
var app = angular.module('myApp', []);
app.controller('mainCtrl', function($scope, $rootScope) {
$scope.medias = [{
"track": 1,
"title": "Glass Jungle 1",
"url": "Glass_Jungle_sample1.mp3"
}, {
"track": 2,
"title": "Glass Jungle 2",
"url": "Glass_Jungle_sample2.mp3"
}, {
"track": 3,
"title": "Glass Jungle 3",
"url": "Glass_Jungle_sample3.mp3"
}];
audiojs.events.ready(function() {
var as = audiojs.createAll();
});
});
app.directive('dynamicUrl', function() {
return {
restrict: 'A',
link: function postLink(scope, element, attr) {
element.attr('src', 'audio_files/' + attr.dynamicUrlSrc);
}
};
});
</script>
其余同理。我没有深入研究您的代码出现问题的原因,自从我使用 angular 以来已经有一段时间了,但我认为这与 myRepeatDirective 有关,它执行了 3 次,一次对于每个音频文件。我猜标记只被复制了两次,因为第二次遇到 javascript 错误以防止它第三次执行。