What is the command using * ngIf to do a larger and smaller check

4

I'm using ngIF just to change the color of the object, but I'm needing to create one that results. MediaTotal is less than 70 and greater than 50. What is the correct syntax for doing this: * ngIf="result.MediaTotal

asked by anonymous 30.10.2017 / 00:22

2 answers

2

I recommend using ngClass instead of ngIf to dynamically style your components. Let's imagine a scenario where you have several styles, you would have to make several identical components just changing their style, this is bad practice because you are repeating code.

Example:

<p [ngClass]="getClass()">{{resultado.mediaTotal}}</p>

Note that we are not doing any validation in html, we can leave that part pro component , where in this example I created the function getClass()

In your Component / Page you would do something like:

 getClass(){
    if(this.resultado.mediaTotal > 50 && this.resultado.mediaTotal < 70)
      return 'my-style-green'
    if(this.resultado.mediaTotal < 50)
      return 'my-style-red'
  }

Note that it is returning a string, this string is your class you can put in the css of your page.

.my-style-green{
     color: green;
 }
 .my-style-red{
     color: red;
 }
    
30.10.2017 / 01:14
3

You should use the logical operator &&

<p 
    *ngIf="resultado.MediaTotal > 50 && resultado.MediaTotal < 70"
    style="color:green">
        {{resultado.MediaTotal}
</p>
    
30.10.2017 / 00:26