How to configure templateUrl as trusted in module configuration?

Consider the 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 throws an exception because services are not allowed during configuration, and I use the $ sce (Strict Contextual Escaping) service here.

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

+7
angularjs
source share
1 answer

Angular has a $sceProvider service, according to which, in privileged contexts, directives and code will be attached to the result of $sce.getTrusted(context, value) , and not directly to the value.

The directives use $sce.parseAs instead of $parse to view the attribute bindings that $sce.getTrusted behind the scenes on volatile literals.

Away from this, I think:


Configuration Blocks — Run during the registration and configuration of the provider. Only suppliers and constants can be entered into configuration blocks. This is to prevent accidental provision of services before they are fully configured.


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


So now $ sceProvider is a built-in service, you cannot enter your own service or built-in services such as $ http in config ().

Decision

Use run () instead.

+1
source share

All Articles