5

I want to change the ui-view templateUrl programatically from inside a controller because the templateUrl option does not support injection of services and I need a service to determine the template URL.

Thank you.

Francisc
  • 77,430
  • 63
  • 180
  • 276

1 Answers1

3

See the docs for templateProvider here:
"Quick Reference - template, templateUrl, templateProvider", Github - angular-ui/ui-router

Mark Rogers
  • 96,497
  • 18
  • 85
  • 138
Nate Abele
  • 5,771
  • 32
  • 27
  • How can I change it from JS though? In the templateProvider I can only inject constants and providers. I can't inject services which I need to determine URL. – Francisc Dec 13 '13 at 20:55
  • `templateProvider: function(serviceName) { return serviceName.doWhatever(); }` – Nate Abele Dec 16 '13 at 16:22
  • You can't inject services in providers, can you? – Francisc Dec 16 '13 at 18:42
  • `templateProvider` is a provider for the template path. It accepts normal injectable services. – Nate Abele Dec 17 '13 at 02:06
  • Hm, I checked that before posing and it failed, but it might have been for other reasons. I'll give that a try, thank you. – Francisc Dec 17 '13 at 09:29
  • Now I remember, the problem with `templateProvider` is that it returns HTML string instead of URL. I need to construct a URL, not HTML. – Francisc Dec 19 '13 at 14:35
  • So inject `$http` and load a template. – Nate Abele Dec 21 '13 at 05:19
  • I thought of AJAX of course, but that's more of a workaround I'd say. – Francisc Dec 22 '13 at 00:16
  • I don't see how it makes any sense to consider that a 'workaround' when [that's exactly what `$templateFactory` does anyway](https://github.com/angular-ui/ui-router/blob/master/src/templateFactory.js#L85). – Nate Abele Dec 25 '13 at 06:05
  • Well, as I see it, if there's a `template` and a `templateProvider`, there should be a `templateUrl` and a `templateUrlProvider`. However, yours is the best solution from what I've been able to read. Please add that to your answer (i.e. using `$http` to return a URL) so I can mark it as correct. Thank you. – Francisc Dec 25 '13 at 15:07