javascript - Multiple occurrences of the same custom directive Angular2 -


i'm building angular application, trying make code highly reusable of directives(as components).

<div class="container container-fluid" >   <div class="row">     <table class="table table-responsive table-hover">       <thead class="thead-inverse">       <tr>         <th *ngfor="let column of columns">{{column.header}}</th>       </tr>       </thead>       <tbody>       <tr *ngfor="let row of rows;trackby: trackbyid;">         <td *ngfor="let column of columns">{{row[column.field]}}         </td>       </tr>       </tbody>     </table>   </div>   <xfd-progressbar></xfd-progressbar>   <div class="row col-lg-offset-5">     <ngbd-pagination-basic></ngbd-pagination-basic>   </div> </div> 

the above directive created have reusable table. general requirement i'll have 1 table per page.

here's corresponding component same -

import {column} "../models/column"; import {component, oninit} "@angular/core"; import {paginationservice} "../services/paginationservice"; import {gridloadutil} "../util/gridloadutil"; import {progressbarservice} "../services/progressbarservice";  /**  * created ppandey on 6/12/2017.  */  @component({     selector: 'xfd-table',     templateurl: './table.component.html',     styleurls: ['./table.component.css'] }) export class tablecomponent implements oninit{   columns: column[]= [];   rows: any[] = [];    constructor(private paginservice: paginationservice,             private tableloader: gridloadutil,             private progressbarservice: progressbarservice) {     this.paginservice.itemstodisplaychange.subscribe((value) => {       this.rows = value     });      this.tableloader.columnschange.subscribe((values) => {       this.columns = values;     });   }    ngoninit() {     this.tableloader.beforedataloaded();     this.progressbarservice.startprogress();   } } 

as can see, component uses services(gridloadutil), provide data, makes code highly decoupled.

here's got pickle. if want have multiple tables on single page, different data , structure, can't service each component tied singleton.

i can choose not inject service, , provide each service setter, in case, need create subclasses each service, didn't need to. also, code become coupled.

is there direction guys think can take solve problem?

thanks, pratik

you can create directive provide service there. angulars di find provider when looks tableloader service, because it's closest component (if component doesn't provide 1 itself):

@directive({   selector: [tableloader1],   providers: [{provide: gridloadutil, useclass: gridloadutil1}], }) class tableloader1directive {}  @directive({   selector: [tableloader2],   providers: [{provide: gridloadutil, useclass: gridloadutil2}], }) class tableloader2directive {} 

then can use

<xfg-table tableloader1></xfg-table> <xfg-table tableloader2></xfg-table> 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -