Skip to content

Unable to inject templateProvider with resolved values #330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sqs opened this issue Aug 17, 2013 · 21 comments · Fixed by #1934
Closed

Unable to inject templateProvider with resolved values #330

sqs opened this issue Aug 17, 2013 · 21 comments · Fixed by #1934
Labels
Milestone

Comments

@sqs
Copy link

sqs commented Aug 17, 2013

A view's templateProvider is unable to be injected with resolved values. The following will fail with an error like Error: [$injector:unpr] Unknown provider: barProvider <- bar.

$stateProvider.state('foo', {
  resolve: {bar: function() { return 123; }},
  views: {
    foo: {
      templateProvider: function(bar) { /* ... */ },
    }
  },
})

I see in resolveState that $view.load only gets some locals to inject. Is there any technical reason why templateProvider couldn't be injected with all of the resolve values in the same way that controller is?

(This is on git master, and I remember seeing the same limitation a couple of months ago.)

@MrOrz
Copy link

MrOrz commented Aug 28, 2013

Just encountered this issue. I need to determine which template to load by looking at the resolved data. However currently in template provider function I cannot access such information.

By the way, in the documentation it mentions that templateProvider can "access to locals", however I could not figure out what this means.

@ksperling
Copy link
Contributor

The reason this works for controller is that the controller is actually instantiated later as a second step, whereas currently $template is actually itself loaded as part of the resolve. It should still be possible to get values to flow through though with some refactoring... essentially a templateProvider would have to be added to the resolve as is (so resolve can inspect it's parameters), rather than called indirectly via $templateFactory.

@MrOrz
Copy link

MrOrz commented Aug 28, 2013

Thanks for the speedy response!

It seems that the controller is the only place we can access resolved data so far.
Maybe the only work-around is to put ng-include in my view, and determine what view to load via ng-include from within the controller.

@ksperling
Copy link
Contributor

I'm not 100% sure this will work, but you should be able to simply have '$template' inside your resolve yourself. It needs to resolve to the loaded template data (not 100% $state won't overwrite that value but give it a try).

@sqs
Copy link
Author

sqs commented Aug 28, 2013

Thanks for the tip. I couldn't get that $template trick to work. The value appears to be overwritten. Also, it would not inject another resolved value (which I think depends on #196).

@nateabele
Copy link
Contributor

So, #196 has now been implemented, which enables resolve inheritance, but for this to work, resolve values would have to be resolved synchronously, before templateProvider was executed, which is a performance decrease that many people won't need.

This will be difficult to refactor this effectively, because of the number of abstraction boundaries it crosses.

@jachenry
Copy link

I have a similar issue where I'd like to load different views/controllers based on a users role. In order for this to work I'd first have to retrieve the roles from the server (most likely as a resolve property) and inject it into the templateProvider and controllerProvider. Only other option is to define two states, one for each role, and redirect accordingly in the state change handler.

@a-laughlin
Copy link

Currently using ui-router 0.2.7 and angular 1.2.6. Currently getting started with angular. I've been debugging what was going on in a UI with multiple named and abstract views. One of the views had a templateProvider fn that returned the template based on resolve values (amidst 8 other values). I didn't know to search for this issue because the templateProvider fn fails silently and I thought the problem was in another part of the code.

Is this failing silently because the return value here is null? I'd be happy to PR an error message if that's a good spot to include it.

@nateabele
Copy link
Contributor

Is this failing silently because the return value here is null? I'd be happy to PR an error message if that's a good spot to include it.

@a-laughlin Yeah, agreed. Thanks.

@a-laughlin
Copy link

Cool. It's going to be a few days before I'll be able to get to it, but I'll send a PR when done.

@mklobusnik
Copy link

Any idea when this will be fixed. Or what is the way around it?

a-laughlin added a commit to a-laughlin/ui-router that referenced this issue May 8, 2014
Unfortunately time has been sparse and this is a low priority.  I haven't tested it as thoroughly as I'd like, but wanted to follow through on the pull request offered in angular-ui#330 (comment).  Hopefully this will help others trying the same thing and getting silent errors.
@hrajchert
Copy link

This issue still persists. I have a route that defines a resource that can be of 3 different types, I don't know the type until I resolve getting data from the db and then I can select which is the correct view and controller.

Is this planned for fixing?

@manar007
Copy link

Guys please fix that, its very important part

@NicoleY77
Copy link

+1

1 similar comment
@BogdanDamianC
Copy link

+1

@nateabele
Copy link
Contributor

I'l fix it when I have time. In the meantime, you're welcome to submit a PR.

@webgago
Copy link
Contributor

webgago commented May 4, 2015

Hey guys,
Have a look at ^^^ please.
Maybe I've missed something, but seems it works

@lalunamel
Copy link

+1

@nateabele
Copy link
Contributor

@mkoryak Does the patch in #1934 (which went out in the 0.2.15 release) not work for you?

@mkoryak
Copy link

mkoryak commented Dec 1, 2015

bah, sorry, i made a comment on the wrong issue!

@CosmoScriv
Copy link

Works like a charm thanks for improving on this. #1934

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.