本文介绍了$ SCE:ITYPE试图在需要字符串内容信任一个非字符串值:上下文背景:resourceUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想播放存储在我的船帆服务器歌曲。 Path是的http://本地主机:4000 /图像/ 123.mp3

I want to play songs stored in my sails server. Path is http://localhost:4000/images/123.mp3.

在前端,我使用NG-重复列出歌曲的服务器。

In front end, i'm using ng-repeat to list that songs from server.

 <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>

这音源导致外部资源的问题。

This audio source cause external resource problem

<audio ng-src="tones.tonePath"></audio>

在角器,我使用$ SCE

in angular controller, i'm using $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导​​致

Without using $sce that cause

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

这是我的JSON从服务器

This is my JSON from server

 [{
    "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-重复播放外部MP3。帮帮我吧。

Then how to play external mp3 in my ng-repeat. Help me.

推荐答案

我找到了解决办法:
External资源不可被AngularJs

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

然后指定NG-src中的过滤器:

Then specify the filter in ng-src:

   <audio 
        ng-src="{{tones.tonePath | trusted}}" />
    </audio>

感谢您的答复。

这篇关于$ SCE:ITYPE试图在需要字符串内容信任一个非字符串值:上下文背景:resourceUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:05