angularjs - $sce :itype Attempted to trust a non-string value in a content requiring a string: Context: resourceUrl

标签 angularjs angularjs-ng-repeat sails.js ngsanitize

我想播放存储在我的帆服务器中的歌曲。路径是 http://localhost:4000/images/123.mp3 .

在前端,我使用 ng-repeat 列出来自服务器的歌曲。

 <div ng-repeat="tones in ringTones track by $index">
      <div>
        <i ng-show="playpause" class="fa fa-play-circle"   ng-click="playpause=!playpause" onclick="plays(event);"><audio id="audio_{{$index}}" ng-src="tones.tonePath"></audio></i> 
        <i ng-show="!playpause" class="fa fa-pause"   ng-click="playpause=!playpause" onclick="stop(event);"></i></div>

</div>

此音频源导致外部资源问题
<audio ng-src="tones.tonePath"></audio>

在 Angular Controller 中,我使用的是 $sce
$http.get("http://localhost:4000/songs/find").success(function(data){
        $rootScope.ringTones=data;
        $rootScope.ringTones.push($sce.trustAsResourceUrl(data[0]));
 }).error(function(data){
                    console.log('ERROR');
 });

错误是:
Error: [$sce:itype] Attempted to trust a non-string value in a 
    content requiring a string: Context: resourceUrl

不使用 $sce 导致
Error: [$interpolate:interr] Can't interpolate: tones.tonePath
Error: [$sce:insecurl] Blocked loading resource from url not allowed by $sceDelegate policy.  URL

这是我来自服务器的 JSON
 [{
    "toneName": "2",
    "aboutTone": "2",
    "duration": 2,
    "tonePath": "http://localhost:4000/images/234.mp3",
    "createdAt": "2015-08-03T15:40:58.227Z",
    "updatedAt": "2015-08-03T15:40:58.227Z",
    "id": "55bf8b8a77efb94b32b158c0"
  },
  {
    "toneName": "3",
    "aboutTone": "3",
    "duration": 3,
    "tonePath": "http://localhost:4000/images/123.mp3",
    "createdAt": "2015-08-03T15:45:16.120Z",
    "updatedAt": "2015-08-03T15:45:16.120Z",
    "id": "55bf8c8c77efb94b32b158c1"
  }
]

然后如何在我的 ng-repeat 中播放外部 mp3。帮我。

最佳答案

我找到了解决方案:
External resource not being loaded by AngularJs

  app.filter('trusted', ['$sce', function ($sce) {
        return function(url) {
            return $sce.trustAsResourceUrl(url);
        };
    }]);

然后在 ng-src 中指定过滤器:
   <audio 
        ng-src="{{tones.tonePath | trusted}}" />
    </audio>

感谢您的回复。

关于angularjs - $sce :itype Attempted to trust a non-string value in a content requiring a string: Context: resourceUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31800589/

相关文章:

javascript - 智能表格无法正确排序

javascript - 错误 - 在处理程序中创建了一个 promise ,但没有从中返回

node.js - 如何获取解码后的 JSON Web Token (JWT) 的值

node.js - 为什么 sails.js 的真实电子邮件地址验证失败

javascript - 与 IE7 的兼容性

javascript - $http 服务 Angular 不工作

javascript - Angular Form 的自定义验证 - 模糊

javascript - 为什么带有 ng-repeat 的指令是从表中编译出来的?

javascript - Angular ng-repeat $index 在 mozilla firefox 上的 ng-if 指令中不起作用

javascript - 将 ng-click 绑定(bind)到动态数组上的 ng-repeat