Ionic2 + Angular2 - dynamic rate value with ion-icon star

sandrina-p

I'm trying to build a simple dynamic rate from 0 to 5 stars (and its middle values like x.5 [example 4.5] ) that receives a value from the javascript.

I looked for something with *ngFor but I'm not understanding how that works. Can someone explain / help me?

If it helps, for ionic, we have 3 type of stars available:

<ion-icon name="star"></ion-icon>
<ion-icon name="star-half"></ion-icon>
<ion-icon name="star-outline"></ion-icon>

For example if I receive from server a value rate = 3.5, it renders:

<ion-icon name="star"></ion-icon>
<ion-icon name="star"></ion-icon>
<ion-icon name="star"></ion-icon>
<ion-icon name="star-half"></ion-icon>
<ion-icon name="star-outline"></ion-icon>

I'm using javascript, no typescript.

Thank you so much :)

p.s. not sure if this title is the better, any suggestion is welcome :)

wdickerson

Here's one way to do it:

<ion-item>
  <ion-icon *ngIf="myRating>=1" name="star"></ion-icon>
  <ion-icon *ngIf="myRating>=2" name="star"></ion-icon>
  <ion-icon *ngIf="myRating>=3" name="star"></ion-icon>
  <ion-icon *ngIf="myRating>=4" name="star"></ion-icon>
  <ion-icon *ngIf="myRating>=5" name="star"></ion-icon>
  <ion-icon *ngIf="myRating%1!=0" name="star-half"></ion-icon>
  <ion-icon *ngIf="myRating==0" name="star-outline"></ion-icon>
  <ion-icon *ngIf="myRating<=1" name="star-outline"></ion-icon>
  <ion-icon *ngIf="myRating<=2" name="star-outline"></ion-icon>
  <ion-icon *ngIf="myRating<=3" name="star-outline"></ion-icon>
  <ion-icon *ngIf="myRating<=4" name="star-outline"></ion-icon>
</ion-item>

It takes up more space in the HTML, but doesn't require any additional javascript. Here, myRating is the star value. I tested it for all 11 possible values.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting dynamically created ion-input value in IONIC2

From Dev

Getting dynamically created ion-input value in IONIC2

From Dev

Ionic 2 - Cannot add an ion-icon inside an ion-item containing a ion-input

From Dev

Angular2 pipe dynamic value not taking

From Dev

Switch icon in ionic2 after click

From Dev

Menu icon not visible in ion-navbar Ionic 2

From Dev

Accessing ion-radio values in Ionic2 Form

From Dev

Ionic2 select text in ion-searchbar

From Dev

ion-label is not displaying whole text in Ionic2

From Dev

Ionic2: Reload the ion content when back button is pressed

From Dev

Adding horizontal scrollbar to Ionic2's ion-slides

From Dev

How to change the color of <ion-card> using ionic2

From Dev

Dynamic form validation in ionic2

From Dev

Angular2 & Ionic2 : Binding value from NavParams to Form value

From Dev

Angular2 & Ionic2 : Binding value from NavParams to Form value

From Dev

Angular2: [(ngModel)] with ion-select - show div depending on select value

From Dev

how to pass dynamic values in star rating plugin in angular 2?

From Dev

Ionic2 selcted value undefined

From Dev

Can I define ion-title outside ion-navbar (i.e. in ion-content) with ionic2?

From Dev

Angular2 /Ionic2: databinding

From Dev

Angular2 dynamic templating

From Dev

Ionic2: How to use an different ion-menu in each child view

From Dev

How can I get the wrapped input from an ion-input in Ionic2?

From Dev

Ionic2 Unhandled Promise rejection: Template parse errors: 'ion-item-content' is not a known element: error

From Dev

Cannot set selected attribute for dynamically loaded Ionic2 ion-select

From Dev

Customize ion select on Ionic 2

From Dev

How to Check value in JSON object ionic2

From Dev

Cannot assign Cordova plugin returned value to views in Ionic2

From Dev

Pass variable value to another component in ionic2

Related Related

  1. 1

    Getting dynamically created ion-input value in IONIC2

  2. 2

    Getting dynamically created ion-input value in IONIC2

  3. 3

    Ionic 2 - Cannot add an ion-icon inside an ion-item containing a ion-input

  4. 4

    Angular2 pipe dynamic value not taking

  5. 5

    Switch icon in ionic2 after click

  6. 6

    Menu icon not visible in ion-navbar Ionic 2

  7. 7

    Accessing ion-radio values in Ionic2 Form

  8. 8

    Ionic2 select text in ion-searchbar

  9. 9

    ion-label is not displaying whole text in Ionic2

  10. 10

    Ionic2: Reload the ion content when back button is pressed

  11. 11

    Adding horizontal scrollbar to Ionic2's ion-slides

  12. 12

    How to change the color of <ion-card> using ionic2

  13. 13

    Dynamic form validation in ionic2

  14. 14

    Angular2 & Ionic2 : Binding value from NavParams to Form value

  15. 15

    Angular2 & Ionic2 : Binding value from NavParams to Form value

  16. 16

    Angular2: [(ngModel)] with ion-select - show div depending on select value

  17. 17

    how to pass dynamic values in star rating plugin in angular 2?

  18. 18

    Ionic2 selcted value undefined

  19. 19

    Can I define ion-title outside ion-navbar (i.e. in ion-content) with ionic2?

  20. 20

    Angular2 /Ionic2: databinding

  21. 21

    Angular2 dynamic templating

  22. 22

    Ionic2: How to use an different ion-menu in each child view

  23. 23

    How can I get the wrapped input from an ion-input in Ionic2?

  24. 24

    Ionic2 Unhandled Promise rejection: Template parse errors: 'ion-item-content' is not a known element: error

  25. 25

    Cannot set selected attribute for dynamically loaded Ionic2 ion-select

  26. 26

    Customize ion select on Ionic 2

  27. 27

    How to Check value in JSON object ionic2

  28. 28

    Cannot assign Cordova plugin returned value to views in Ionic2

  29. 29

    Pass variable value to another component in ionic2

HotTag

Archive