[BUG] p-table: NullInjectorError when passing an ng-template in to a component which wraps a p-table
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
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
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.
@cagataycivici this is still an issue in the current version of prime-ng. it's not a support-request or question, it's basically a bug report with details. Or are you not planning on providing a solution?
I still have this issue with primeng 18 and angular 18
The issue can be resolved by providing Table in the components providers array as follows:
import { Table, TableService } from 'primeng/table';
...
...
providers: [
TableService,
Table
]