2

I've dynamically created an Angular 2 component using DynamicComponentLoader.loadAsRoot(), and am trying to give it a useful template (involving NgFor), but this is somehow yielding me a 'DI exception' dependency injection error.

I had already injected NgFor itself though, so specifically now it gave me a No provider for IterableDiffers! (NgFor -> IterableDiffers). So I injected that, which got me to a No provider for Array! (NgFor -> IterableDiffers -> Array). At this point I got stumped; Array is a base type rather than something part of Angular, and somehow trying to similarly inject it did not work (error: Array is a generic; must specificy some sub-type). I'm confused but I'd be surprised if basic types suddenly required injection.

I made a Plunker to recreate my issue here. Interestingly on Plunker the DI exception turned out as No provider for e! (e -> e) instead. Not sure why the difference, but the principle issue would seem similar... though e gives me even less useful info to go by than Array. :(

How might I prevent such exceptions here?

4
  • Update: on the angular gitter @ericmartinez helped me out, noting the DI errors could be prevented by passing an injector to loadAsRoot(), while actually getting the NgFor result to show would require adding a timeout to ChangeDetectorRef.detectChanges() in the child component... which even if it works seems like an unfortunate hack, so still trying to figure out whether that's as it should be... Commented Nov 30, 2015 at 5:23
  • did you report an issue on github? Commented Jan 13, 2016 at 0:41
  • I created github.com/angular/angular/issues/6444 Commented Jan 13, 2016 at 0:46
  • Sorry, I was aware of their existing issue. Hope it'll get fixed though; manually calling detectChanges works but is pretty annoying. Not to mention hella confusing when you're not yet aware what went wrong or how to address it... Commented Jan 13, 2016 at 13:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.