user1272965 user1272965 - 6 months ago 158
AngularJS Question

Use $compileProvider to avoid unsafe links

I have no trouble injecting things like $scope and $location and $routeProvider, why is $compileProvider different?

Based on this answer, I understand that I have to instruct angular to not prefix certain links (sms in my case), but I can't apply the answer in my project. It says I should add this:

angular.module('myModule', [], function ($compileProvider) {
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file):/);
});


But the chrome console says:


"angular.js:68 Uncaught Error: [$injector:unpr] Unknown provider:
$compileProviderProvider <- $compileProvider"


That "provider-provider" thing made me think that the real name of the service is just $compile (and that angular is tacking on the "provider" suffix:

angular.module('myModule', [], function ($compile) {
$compile.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file):/);
});


But then, predictably, I guess, I get:


angular.js:13550 TypeError: $compile.aHrefSanitizationWhitelist is not a function

Answer

That's because you have to add it as a config:

angular.module('myModule').config(['$compileProvider',
  function($compileProvider) {
    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file):/);
  }
]);

Comments