简体   繁体   English

使用ngSanatize进行HTML5音频和Angular双向绑定的问题

[英]Problems with HTML5 audio and Angular two way binding using ngSanatize

I'm trying to create a dynamic playlist that pulls in an .mp3 file from an array and places it dynamically into an audio tag. 我正在尝试创建一个动态播放列表,从数组中提取.mp3文件并将其动态放入音频标记中。 Check out the demo here: 看看这里的演示:

http://plnkr.co/edit/NYKwAY?p=preview http://plnkr.co/edit/NYKwAY?p=preview

At first I got console errors stating that: 起初我得到了控制台错误,说明:

Blocked loading resource from url not allowed by $sceDelegate policy $ sceDelegate策略不允许阻止从url加载资源

But I got around that by using ngSantatize and the console didn't throw those errors anymore. 但是我通过使用ngSantatize解决了这个问题,并且控制台不再抛出这些错误了。 The only problem is that the mp3 file STILL doesn't want to play. 唯一的问题是mp3文件STILL不想播放。 The only error the console gives me is from the angular-audio-player: 控制台给我的唯一错误来自角度音频播放器:

if you use playlist attribute, you need $scope.playlistVariable = []; 如果您使用播放列表属性,则需要$ scope.playlistVariable = []; in your code 在你的代码中

Any ideas on how to make this work in Angular? 关于如何在Angular中完成这项工作的任何想法? Am I implementing ngSanatize incorrectly? 我是否错误地实施了ngSanatize?

I think the prob is that you need to return $sce.trustAsResourceUrl(url) as the thing that gets pulled into src. 我认为问题是你需要返回$sce.trustAsResourceUrl(url)作为被拉入src的东西。 Also, ng-src will keep away momentary errors trying to load "{{whatever}}" . 此外, ng-src将避免尝试加载"{{whatever}}"瞬间错误。

I decided to try out making one from scratch , using ionic as you have, to sort of play around with some simple concepts. 我决定尝试从头开始制作一个 ,使用离子,你可以尝试使用一些简单的概念。 It loads a NPR feed, and display a nice playlist. 它加载NPR Feed,并显示一个漂亮的播放列表。 I don't know if I would call this the "right way", but I chose to simplify your setup by binding very little management, and trying to get angular to do more of the work. 我不知道我是否会称之为“正确的方式”,但我选择通过绑定很少的管理来简化您的设置,并试图获得更多角度来完成更多的工作。 It's not perfect, but this should get you started. 这不是完美的,但这应该让你开始。

In general, in any framework, I generally decide that if I have a lot of "management code" setting & getting things all over, etc, I am probably not using it right. 一般来说,在任何框架中,我通常会认为,如果我有很多“管理代码”设置和全部事情,等等,我可能没有正确使用它。 I try to figure out the "smell" of good code, or the basic style of people who are good examples in the community, to get an idea of how to do stuff efficiently. 我试图弄清楚好的代码的“气味”,或者社区中很好的例子的人的基本风格,以了解如何有效地做事。 In angular, it seems like the main goal is to keep business logic out of HTML, but reusable UI logic as directives (attributes & elements) with minimal Controllers to glue it together, and services to provide data. 在角度方面,似乎主要目标是将业务逻辑保持在HTML之外,但可重用的UI逻辑作为指令(属性和元素),使用最少的控制器将它们粘合在一起,以及提供数据的服务。

Also, here is an excellent directive that does what I think you are hoping to accomplish. 此外, 是一个出色的指令,可以完成我认为您希望实现的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM