-
Notifications
You must be signed in to change notification settings - Fork 27.4k
templateUrl with function, hardcoded $element and $attrs #13526
Comments
The injector issue is fixed by #13485. The interpolation issue is probably not easily fixable, if at all, as interpolation only happens in the link phase, and the scope is not yet available when the template is requested. So it's just not possible at the moment to evaluate the value before the template is requested. See here for more discussion: #2895 Generally, I think something like a language parameter to a directive is a bad idea anyway. You'd have to add that to any directive in your application. My suggestion is to provide a requestTransform to the templateRequestProvider that modifies the url to include the language parameter based on a service value. With dynamic templateUrls you are also losing the advantage of preloading your templates into the templateCache in a build step / when you first request the app. |
@Narretz I've been searching for the solution you've just suggested for a while now (see my post from the very issue #2895 you quoted). Your transformRequest idea is great. I want it! I just have no idea how to make it work. I hit the same dead end as this guy #2063 From the docs
Doesn't say too much about what format the return value should be, and certainly no tips on how to modify just one thing like the url. My code so far...
Could you please give me a steer in the right direction? Or did I misinterpret you, and you're in fact suggesting that a new issue be created to add this functionality to Angular? |
Ah, I was thinking of interceptors, not requestTransform. However, they are not available for templateRequestProvider ... But, no worries! You can use the httpProvider for that. In this plnkr, I am adding a special property to all templateRequests configs, and then in the interceptor I am adding a prefix: http://plnkr.co/edit/DNvTL5jogbgyb0BqXD5C?p=preview You could also filter the url by ending, by path etc. Depends where your templates are coming from and if you need prefixing for all of them. |
This is awesome @Narretz. Thank you very much! Now I've just gotta surmount this problem (my |
You can inject into the interceptors! https://code.angularjs.org/1.5.0-rc.0/docs/api/ng/service/$http#interceptors |
That's magic. Thanks again! :) That's part of Angular that I really haven't understood until now. If only your examples were in the doc... |
I'm trying to use an element
and in the
.component
definition use the function form oftemplateUrl
to add thetplsuffix
to the end of the template url, thusMy problems are
$element
and$attrs
or the .component blows up. I imagine this means that minifying will kill this functionality, and it seems['$element', '$attrs', function ... ]
notation isn't allowed here."{{user.lang}}"
instead of the value of that variable.The relevant documentation in rc0 doesn't discuss how to use this nor give a
templateUrl
function example.Is this a bug? Or wasn't this function form designed to handle my use case? Or can someone please provide the correct usage if I've just got it wrong? Thanks!
The text was updated successfully, but these errors were encountered: