I need to sort components dynamically in the following template
<div class="orderList">
<componentA></componentA>
<componentB></componentB>
<componentC></componentC>
</div>
by the object like
orders: {
componentA: 2,
componentB: 3,
componentC: 1
}
So I expect to see componentC at first then componentA and finally componentB?
Notes: components are more than three
A higher-order component (HOC) is an advanced technique for reusing component logic. Concretely, a higher-order component is a function that takes a component and returns a new component.
Components are the most basic UI building block of an Angular app. An Angular app contains a tree of Angular components. Angular components are a subset of directives, always associated with a template. Unlike other directives, only one component can be instantiated for a given element in a template.
What is a Selector in Angular? A selector is one of the properties of the object that we use along with the component configuration. A selector is used to identify each component uniquely into the component tree, and it also defines how the current component is represented in the HTML DOM.
NgComponentOutletlink Instantiates a Component type and inserts its Host View into the current View. NgComponentOutlet provides a declarative approach for dynamic component creation.
As @GünterZöchbauer suggested in comments this answer uses ngComponentOutlet
to sort components dynamically
<ng-container
*ngFor="item in sortedComponent"
*ngComponentOutlet="item.component">
</ng-container>
sortedComponent = [
{index: 4, component: 'componentA'},
{index: 5, component: 'componentB'},
{index: 2, component: 'componentC'},
].sort((a, b) => a.index > b.index)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With