Search code examples
javascriptangularjsrequirejsangular-directive

Angular + RequireJs: Resolve templateUrls of directives


I use angularjs and requirejs in my spa. For the organization of imports and so on I use require. In requirejs I can use e.g. baseUrl: Every import path is resolved with the baseUrl. Now I would like to resolve the templateUrls the same way. Therefore I can use e.g.:

templateUrl = requirejs.toUrl("modules/test/chuck.directive.html")

The problem that I would like to resolve every templateUrl of every directive this way.

So: Is there a possibility to jump into the template loading process of directives in angular and run the above code?

Thanks for any hint.


Solution

  • I would decorate $templateRequest service if you know for sure that you want to intercept all template loading requests and modify template URL. Something like this:

    .config(function($provide) {
        $provide.decorator('$templateRequest', function($delegate) {
            return function(tpl, ignoreRequestError) {
                tpl = requirejs.toUrl(tpl); // modify original tpl
                return $delegate.call(this, tpl, ignoreRequestError);
            };
        });
    })