1

From Angular documents it says that $sceDelegateProvider.resourceUrlWhitelist can set the trusted resource URLs, But I need that functionality from the controller.

I want to set a whitelist of trusted resource URLs within my service. Therefore, I am only allowed to use $sce service and not provider..

I was trying to implement this example and it caused me an error ( "$sce:unsafe Require a safe/trusted value"):

this.renderUrl = this.$sce.getTrustedUrl(${this.baseUrl}) + `/x/y/?name=${this.name};

Then inject the the renderUrl into the html:

<iframe ng-src="{{$ctrl.renderUrl}}"></iframe>

Is there a way to set a trusted resource URLs from $sce service, so it will be added to the whitlist of trusted resource URLs?

1 Answer 1

1

Create a Filter :

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

In your service:

// Injecting Filter in Service
app.service('trustUrlService', function ($scope,$filter) {

  $scope.trustUrl = function(url){
    return $filter('trusturl')(url);
  };

});

In Controller use like this:

app.controller("myCtrl", function($scope,trustUrlService) {  
  $scope.url = trustUrlService.trustUrl(url);
});

In Template you could use like this:

<iframe ng-src={{ imageHref | trusturl }}" />
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks I am aware of that option. I am looking for a way it will be much more generic in case I will have the same prefix of base url. $sce.trustAsResourceUrl('example.com/url'); limits me and force me to call that method each time for the same base url.
@Moris Updated my answer
is there any specifice requirement to use it from the service?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.