Primeng: [BUG] p-table: NullInjectorError when passing an ng-template in to a component which wraps a p-table

Created on 23 Jul 2019  路  3Comments  路  Source: primefaces/primeng

I'm submitting a ... (check one with "x")

[x] bug report => Search github for a similar issue or PR before submitting
[ ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35

Plunkr Case (Bug Reports)
https://stackblitz.com/edit/github-3cegwc-qnslnw

Current behavior
I have created a wrapper component which wraps a <p-table> and I use <ng-template> to pass in a header and body template to this wrapper component. This all works fine unless I use <p-tableHeaderCheckbox> or <p-tableCheckbox>. At this point I get a NullInjectorError. This error also occurs when I try to use the editable row functionality.

Error: StaticInjectorError(AppModule)[ScrollableView -> Table]: 
StaticInjectorError(Platform: core)[ScrollableView -> Table]: 
NullInjectorError: No provider for Table!

Expected behavior
The error is not thrown.

Minimal reproduction of the problem with instructions
https://stackblitz.com/edit/github-3cegwc-qnslnw

What is the motivation / use case for changing the behavior?
This would allow for a reusable component wrapping <p-table> to be created.

  • Angular version: latest

  • PrimeNG version: latest

  • Browser: all

  • Language: all

Most helpful comment

Thanks, @Rillist - I found your StackOverflow post while I was looking to solve this problem and it works, but it would be nice if this just worked so future users didn't need to go through this headache.

All 3 comments

I was able to resolve your error by adding some providers to the wrapper component. I created a wrapper like this last December and had similar issues.

My SO post explains my journey through this, https://stackoverflow.com/a/53961309/10843675

But I also forked your stackblitz and fixed it as an example for you, https://stackblitz.com/edit/table-wrapper-with-providers

The key to this working is the factory function. It should provide a class instance of a Table, but it needs to be the correct instance that your component wrapped. So the factory function requires your wrapper component as a dependency, and then just returns the ViewChild reference inside of it.

Somehow Angular knows to provide the correct wrapper to the factory function, honestly I am not sure how that part works but it definitely does. I've used multiple instances of the same wrapper on the same page and had no issues with the references.

Thanks, @Rillist - I found your StackOverflow post while I was looking to solve this problem and it works, but it would be nice if this just worked so future users didn't need to go through this headache.

Please use forum for support requests and questions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

philly-vanilly picture philly-vanilly  路  3Comments

just-paja picture just-paja  路  3Comments

cyberrranger picture cyberrranger  路  3Comments

markgoho picture markgoho  路  3Comments

watalberto picture watalberto  路  3Comments