I'm trying to make a generic component to display list resources. I'm running into issue instantiating the component in HTML. I was using this answer to attempt to fix the problem but I feel it will not work.
I have this Component
<ion-list>
<ion-item-sliding *ngFor="let entity of entityList" #item>
<ion-item (click)="navigateToDetail(entity.id)">
<ion-avatar item-left>
<img src="http://modexenergy.com/wp-content/themes/modex_wp/img/avatar.png">
</ion-avatar>
<h2>{{ entity.email }}</h2>
<ion-icon name="arrow-forward" item-right></ion-icon>
</ion-item>
<ion-item-options side="right">
<button ion-button color="danger" (click)="delete(entity.id)">
<ion-icon name="trash"></ion-icon>Delete
</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
export class PageListBaseComponent<T extends IHasId> {
@Input() entityType: T;
@Input() detailPageType: any;
public entityList: T[];
constructor(public navCtrl: NavController, public navParams: NavParams, public baseProvider: ProviderBase<T>) {
baseProvider.getList().subscribe(entities => {
this.entityList = entities;
console.log(entities);
});
}
delete(id: number) {
console.log(id);
//this.baseProvider.deleteById(id).subscribe(result => {
// console.log(result);
//});
}
navigateToDetail(id: number) {
console.log('test ' + id)
this.navCtrl.push(this.detailPageType, { id })
}
}
And I initialize it from my users page like so:
<ion-content padding>
<page-list-base [entityType]="userType" [detailPageType]="userDetailType">
</page-list-base>
</ion-content>
export class UsersPage {
public userType: User;
public userDetailType: ProfilePage;
constructor(public navCtrl: NavController, public navParams: NavParams) {
}
}
This does not work because my Component references the public baseProvider: ProviderBase<T>
in the constructor the Dependency Inject cannot resolve the type.
I would like to be able to reuse this as much as possible. How can I initialize this generic component properly? If that is not possible, how can I late grab out the baseProvider after T resolves?
I solved this by delaying the initialization, and making the dependency a
@Input
propertyThen I Modify the Page to take in a concrete implementation of our dependency,
Then I implement the component like so: