$sce:itype 尝试信任需要字符串的内容中的非字符串值:上下文:resourceUrl

2024-03-25

我想播放存储在我的 sails 服务器中的歌曲。路径是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>

在角度控制器中,我使用 $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。帮我。


我找到了解决方案:AngularJs 未加载外部资源 https://stackoverflow.com/questions/21292114/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>

感谢您的回复。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

$sce:itype 尝试信任需要字符串的内容中的非字符串值:上下文:resourceUrl 的相关文章

随机推荐