Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup templateUrl as trusted on module config?

Tags:

angularjs

Consider code (Angular 1.2 RC3):

main.config(['$routeProvider', '$sce', function($routeProvider, $sce) {
    $routeProvider.when('/', { templateUrl: $sce.trustAsResourceUrl('bla-bla.html'), controller: "App.Controllers.BlaBla" });
    $routeProvider.otherwise({ redirectTo: '/' });
}]);

It will throw an exception because services are not allowed during config and i'm using "$sce" (Strict Contextual Escaping) service here.

How to use SCE in "config" method? What is possible solutions to this problem?

like image 908
Eugene Baranchuk Avatar asked Nov 04 '13 23:11

Eugene Baranchuk


1 Answers

Angular has $sceProvider service whereby in privileged contexts, directives and code will bind to the result of $sce.getTrusted(context, value) rather than to the value directly.

Directives use $sce.parseAs rather than $parse to watch attribute bindings, which performs the $sce.getTrusted behind the scenes on non-constant literals.

Away from that i think:


Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.


Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.


So, now $sceProvider is an inbuilt service, you can't inject your own service, or built-in services like $http into config().

SOLUTION

Use run() instead.

like image 92
Sir Mbuki Avatar answered Nov 14 '22 03:11

Sir Mbuki