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

不打扰是莪最后的温柔 提交于 2019-12-05 00:43:37

问题


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

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>

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

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

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

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"
  }
]

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


回答1:


I found solution : External resource not being loaded by AngularJs

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

Then specify the filter in ng-src:

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

Thanks for response.




回答2:


//first Take data from api 

$scope.Data= JSLINQ(data.Data).Select(function (Item) { return Item; })                    

//Map data as trustable to scope
$scope.Data.items.map(function (i) {
                     i.Header1 = $sce.trustAsHtml(i.Header1);     
                 });

//UI bind modal data

<p ng-bind-html="x1.Header1 " ng-show="x1.Header1 != null"></p>


来源:https://stackoverflow.com/questions/31800589/sceitype-attempted-to-trust-a-non-string-value-in-a-content-requiring-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!