M
M
Masterstvo2017-06-12 14:32:55
Angular
Masterstvo, 2017-06-12 14:32:55

How to iterate and render components in Angular 2?

Greetings! New to Angular. The task is next. There is an array of objects (components that take data from this array) that are rendered using the ngFor structural directive.
What if you want to render not all objects from the above array, but only some. In other words, is it possible to somehow filter the data in ngFor before rendering it. For example, if the property of one of the objects is false, then it is not rendered.
Interested precisely within the framework of structural directives or their interaction. If it is not possible, then tell me how to implement it in a different way.
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RidgeA, 2017-06-12
@RidgeA

https://angular.io/docs/ts/latest/guide/pipes.html

O
ozknemoy, 2017-06-14
@ozknemoy

inside ngFor put tags with ngIf condition

<tr *ngFor="let row of table['1'] | paginate: { itemsPerPage: 5, currentPage: currentPage}">
                                <td data-text="Номер карты" ngIf="row.ref_number">{{row.ref_number}}</td>
                                <td data-text="Личный товарооборот в баллах">{{row.my_bonus}}</td>
                                <td data-text="Товарооборот сети в баллах">{{row.ref_bonus}}</td>
                                <td data-text="Статус">
                                    <label class="label" [ngClass]="{
                                                'label-danger':row.status.id===0,
                                                'label-success':row.status.id===1,
                                                'label-info':row.status.id===2,
                                                'label-warning':row.status.id===3}">{{row.status.name}}</label>
                                </td>
                            </tr>

as a bonus, work with the tag class depending on the condition is shown. if the array is sooo big then i usually do the prep work in the controller

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question