0

I'm using Angular 6.

I would like to assign a local variable in for loop to avoid multiple call of the method.

<li *ngFor="let item of items">
    <!-- Declare someItem variable here -->
    <!-- let someItem = getSomeItemFromDifferentDataStructureUsingSomeLogic(item) -->
    {{ someItem.name }}
    {{ someItem.anotherValue }}
</li>

I don't also want to make a separate component to display someItem cause it is too trivial to create separate one.

What would be your's approach?

Many thanks for a help.

2
  • 1
    Your best approach would be to handle this in your component before displaying it in the html template. Commented Sep 27, 2018 at 20:23
  • No way to do it like you describe. Commented Sep 27, 2018 at 20:25

1 Answer 1

1

I would map items to someItems in compontent code and interate over it

In compontent:

someItems=items.map(this.getSomeItemFromDifferentDataStructureUsingSomeLogic.bind(this));

and in template

<li *ngFor="let someItem of someItems">
    {{ someItem.name }}
    {{ someItem.anotherValue }}
</li>

In case if you need in this *for loop original item, just create an array of item-someItem pairs and iterate over that, using what is needed respectively.

Sign up to request clarification or add additional context in comments.

1 Comment

As I thought. I need to prepare whole data to display in component code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.