artch/angular-route-segment

About Template Url Function

Opened this issue · 2 comments

Hi,
I read the document and it says that templateUrl can be a function, but when I use the function, the error appears. Error: [$injector:unpr]. It works fine in string.
By the way, I wonder if I can use the function to make the segment become a dynamic link because I want to transfer some variable to the server also.
Thanks

Hi, could you write a tutorial about how to use function in templateUrl and controller?
Thanks

Well, 4 years later it probably doesn't matter, but I figured maybe there's another lost soul like me somewhere, stalking hopelessly in the shadow realms of legacy code.
This happens because you either have ng-strict-di="true" or try to use minification. That's when injection fails, thanks to lines 554 or 561. So say you have

templateUrl: function ($routeParams) { return "yoursite/products/" + $routeParams.productId }

When that gets injected after minification/with strict DI, $routeParams is lost. To make it work you have to rewrite line 554 or 561 like this:

locals.$template = $injector.invoke(["$routeParams", locals.$template]);

Consider yourself lucky if all your template functions depend on the same parameter.