I have a reusable section that based on parent's input creates some paragraphs in the DOM. It was working just fine, till the requirements had me take the paragraphs' sequence into account. I solved it the most obvious way and moved on...
My solution involved ngIf, ngSwitch could've been used likewise:
parent template
<button (click)="orderisBADC()">Order BADC</button>
<button (click)="orderisBDCA()">Order BDCA</button>
<button (click)="orderisCBDA()">Order CBDA</button>
<app-child [order]="order"></app-child>
parent component
export class HomeComponent {
order = '';
orderisBADC() {
this.order = 'BADC';
}
orderisBDCA() {
this.order = 'BDCA';
}
orderisCBDA() {
this.order = 'CBDA';
}
}
child template
<div *ngIf="order === 'BADC'">
<p>B</p>
<p>A</p>
<p>D</p>
<p>C</p>
</div>
<div *ngIf="order === 'BDCA'">
<p>B</p>
<p>D</p>
<p>C</p>
<p>A</p>
</div>
<div *ngIf="order === 'CBDA'">
<p>C</p>
<p>B</p>
<p>D</p>
<p>A</p>
</div>
child component
export class ChildComponent implements OnInit {
@Input()
order: string;
}
But I could easily imagine there will be more possible sequences in the future. Is there a smarter way to overcome this, provided that the paragraphs are not enough of an entity themself to be even considered a separate component? Thank you in advance for any suggestions. :)
Edit:
I would like to clarify something.
The A, B, C, D texts are just placeholders. For simplification let's assume that the content we have in child is separated into equivalent block elements but their interior may vary. Some may have inputs, other tables and eventually some are filled with text, but it's in no way equal to the sequence symbols. Only child decides what's inside each element. The question remains the same: how to influence only sequence and/or existance of a viewed content from parent to child?