Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordering components in angular

Tags:

angular

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

like image 410
fingerpich Avatar asked Jul 24 '17 12:07

fingerpich


People also ask

What are higher order components in angular?

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.

What is @component in angular?

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 component selector in angular?

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.

What is NgComponentOutlet?

NgComponentOutletlink Instantiates a Component type and inserts its Host View into the current View. NgComponentOutlet provides a declarative approach for dynamic component creation.


1 Answers

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)
like image 78
fingerpich Avatar answered Oct 19 '22 03:10

fingerpich