A
A
Artem2018-08-07 14:01:51
Angular
Artem, 2018-08-07 14:01:51

How to pass a calculated expression to a component?

There is a component to which the list of elements is transferred. The type and structure of the elements is unknown to the component. These elements need to be grouped and sorted according to rules known only to the parent component.
The obvious way : in the parent component, declare a method that will take one of the elements and return, for example, a string based on which the child component will sort the elements.
As you would like : pass an executable typescript expression to the component with work on the "context". Almost like an arrow function, only arrow functions don't work in angular templates.
Example :

<!-- parent component html -->
<my-component [expression]="$context.foo.bar" [items]="Items"></my-component>

// my-component code
@Input() expression: Function;
@Input() items: any[];
...
const results = this.items.map(item => this.expression(item)); // $context = item

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2018-08-07
@Xuxicheta

In theory, it would be nice to transfer handlers to a service.

<!-- parent component html -->
<my-component [funcName]="'myFunc'" [items]="Items"></my-component>

// my-component code
@Input() expressionName: string;
@Input() items: any[];
...
const results = this.items.map(item => this.service[funcName](item)); // $context = item

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question