I am building an app with a consistent design pattern for lists of elements. If I have an object of type A, I create AComponent which accepts a as an input, then create another component to iterate over a list of A's, AListComponent. Then if I have an object B, I need to do the same thing. It seems like I should be able to make an ObjectListComponent passing in the class of the object I want to iterate over, to keep my code DRY.
For example, given
AComponent Controller
...
@Input
a: A;
...
AComponent HTML
<div>{{ a.name }}</div>
AListComponent Controller
...
@Input()
aList: A[];
...
AListComponent HTML
<div *ngFor='let aObj of aList'>
<app-a [a]='aObj'></app-a>
</div>
How can we abstract the AListComponent to an ObjectListComponent?
ObjectListComponent Controller
...
@Input()
type: any;
@Input()
objects: <type>[]
objectComponent: any;
ngOnInit () {
this.objectComponent = <get object component from type>
}
ObjectListComponent HTML
<div *ngFor='let obj of objects'>
<app-objectComponent [object]='obj'></app-objectComponent>
</div>
where the ObjectListComponent would be used as
...
<app-object-list [type]='A' objects='aList'></app-object-list>
...
AComponentvsBComponent? I mean, are they all similar (css, functions) ? what is the purpose ofthis.objectComponent, how are you planning to use it? If the only diff is thetypethen create interfaceICanvasComponentInputand define type asobj: A | Binside it. use it inObjectListComponent@Inputparam. (Try to give more generic name to ObjectListComponent)ObjectListComponentshould be able to iterate over an arbitrary list of models because the component representing the model is passed in through thetypeattribute.this.objectComponentwould be the component representing the type of objects in the list of objects passed in through theobjectsattribute.