*ngFor with angular2

Asked

Viewed 1,462 times

0

I would like to create a loop of repetition, but in this loop I already have the amount of times it will perform. Below is an example of the expected result using javascript.

for(i = 0; i < 5; i++) {
    console.log( i + 'estrelas!');
}

In Angularjs 2, I believe it would be done like this (it’s just an example, I know it doesn’t work but I’m looking for something similar).

<ion-icon *ngFor="1 to 5" name="star"></ion-icon>

1 answer

3


Following only the pattern of Angular2 the correct syntax would be this:

<div *ngFor="#hero of heroes">{{hero.fullName}}</div>

Where it just replaces the same function of ng-repeat of Angular 1.x, where it will loop the entire length of your array, you don’t need to specify or know what the extension is. If it’s 5, 10 or 300 ngFor will go through all. The difference between the ngFor and the for() that you refer to, is that the ngFor is applied directly in the template, ie if you want to do some manipulation of the data before going to the view, this should be done yet on component responsible, ai yes you can use the for() for only later pass to your view (html).

Everything will depend on your goal.


Edited:

I confess I never used Ionic, but if the logic is the same as using icon-font (which I imagine it is) you can use an ng-class to apply the class corresponding to the amount of votes. For example:

<i [ngClass]="{
    'ion-android-star-outline': valorEstrela < 1, 
    'ion-android-star': valorEstrela >= 1}">
</i>
<i [ngClass]="{
    'ion-android-star-outline': valorEstrela < 2,
    'ion-android-star': valorEstrela >= 2}">
</i>
<i [ngClass]="{
    'ion-android-star-outline': valorEstrela < 3, 
    'ion-android-star': valorEstrela >= 3}">
</i>
<i [ngClass]="{
    'ion-android-star-outline': valorEstrela < 4, 
    'ion-android-star': valorEstrela >= 4}">
</i>
<i [ngClass]="{
    'ion-android-star-outline': valorEstrela < 5, 
    'ion-android-star': valorEstrela >= 5}">
</i>

Thus, you should only assign the value received from the bank to the property valorEstrela and it will apply the class corresponding to the fill. Again, as I have never used Ionic I don’t know if there’s a better option, but I believe that the one I went through should solve your problem in a more practical way.

  • My goal is not to work with vector... It is a scheme for rating, back of the bank an integer 4 for example. I take this whole and I have to print 4 stars on the screen. But it’s an integer not a vector.

  • Then it is not ngFor that will use, as it serves to interact by the objects of an array and display each of them on the screen. To achieve its goal would require the use of another logic.

  • So @Celsomtrindade, I know it’s not, I just used it to expose my need..

  • It’s this other logic that I’m after, I’ve decided to create a vector by allocating the amount of positions needed which is gambiarra =/

  • @Hiagosouza Are you using what to display these stars? Icon font?

  • I’m using the ion-icon directive

  • @Hiagosouza edited my answer, see if that’s what you’re looking for. = D

  • Sorry the delay, although I have not tested the above way arrives at the result I expected at the time. Thank you.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.