【发布时间】:2018-01-27 07:36:12
【问题描述】:
我目前正在构建一个 Angular 应用程序,我试图在指令(作为组件)的帮助下使代码高度可重用。
<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>
以上是我创建的具有可重用表的指令。我的一般要求是每页有一个表格。
这是相同的相应组件 -
import {Column} from "../models/Column";
import {Component, OnInit} from "@angular/core";
import {PaginationService} from "../services/PaginationService";
import {GridLoadUtil} from "../util/GridLoadUtil";
import {ProgressBarService} from "../services/ProgressBarService";
/**
* Created by 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();
}
}
如您所见,我的组件使用 Services(GridLoadUtil) 为其提供数据,这使得代码高度解耦。
这就是我进入泡菜的地方。如果我想在一个页面上有多个表,具有不同的数据和结构,它不能作为每个组件所绑定的服务是 Singleton。
我可以选择不注入服务,并将每个服务作为 setter 提供,但在这种情况下,我需要为每个服务创建子类,目前我不需要这样做。另外,我的代码会耦合。
你们认为我可以采取什么方向来解决这个问题?
谢谢, 普拉提克
【问题讨论】:
标签: javascript angular angular2-services angular2-directives