TLDR:
How do I use one instance of an Angular (6) service for one set of (instances of) components (and directives) and another one for another set of the same components.
Or with more information:
I am currently working on adding a sorting feature to tables in my Angular 6 based application. Because I am using custom styles (materialize based) most libraries aren't working for me. I found this great example though which is totally independent of the styles used.
It creates a SortableColumnComponent
that gets added to each <th>
header and a SortableTableDirective
that gets added to the <table>
element. They communicate via a SortService
that basically just provides a RxJS subject to notify other columns when the sorting direction/attribute changes.
The problem here is that this works great as long as only one sortable table is shown at a time. Once you add more though, sorting can only be applied to one at a time (as they all share the same service).
According to the angular docs a service becomes a singleton automatically when you inject it only into the application root (which I did). So instead I tried to inject it only into the sortableColumn:
@Component ({
selector: '[sortable-column]',
templateUrl: './sortable-column.component.html'
providers: [SortService]
})
but then each column seems to get its own version of the service and can be sorted at the same time (which obviously does not work as intended).
So the overall question is: how can I assign one instance of an angular service to one (instance of a) component (the SortableTableDirective
) and the matching SortableColumnComponent
components and another instance of that same service to other tables.
To maybe make this clearer, this is what I am trying to achieve:
-------------------------------------------------------------------------
| page |
| |
| table 1 - SortableTableDirective - using instance 1 of sortingservice |
| th 1 - sortablecolumncomponent - using instance 1 of sortingservice |
| th 2 - sortablecolumncomponent - using instance 1 of sortingservice |
| .... |
| table 2 - SortableTableDirective - using instance 2 of sortingservice |
| th 1 - sortablecolumncomponent - using instance 2 of sortingservice |
| th 2 - sortablecolumncomponent - using instance 2 of sortingservice |
-------------------------------------------------------------------------
Or is there a way to bind certain column components to the table directive directly and just remove the service? There seems to be this one concept of "linking" that I am missing here.