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

Aravinth picture Aravinth · Aug 4, 2015 · Viewed 15k times · Source

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.

Answer

Aravinth picture Aravinth · Aug 4, 2015

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.