2

I am making users' leaderbord:

 <div class="user">
  <ul id="jogadores" *ngFor="let u of sortedUsers$; let i=index;" (click)="routeToUser(u.id)">
    <li class="user">
      <img class="gravatar" src="https://www.gravatar.com/avatar/{{u.imgUrl}}?d=https://www.1plusx.com/app/mu-plugins/all-in-one-seo-pack-pro/images/default-user-image.png" />
      <div class="progressBar">
        <span style="height: 100%;">
          <i></i>
          <b>{{sortedUsers$[i].points}}</b>

        </span>
      </div>
      <span class="username">
        <strong>{{sortedUsers$[i].username}}</strong>
      </span>
    </li>
  </ul>
</div>

Each <ul> element has some values which are coming from array. All evening I've been trying to change <span style="height: 100%;"> value to some value from {{sortedUsers$[i].points}} so each progress bar would change according to how many points user has. I tried passing value:

<span style="height: {{sortedUsers$[i].points}} %;">

also reaching it with jQuery, but also failed.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user122222
  • 2,179
  • 4
  • 35
  • 78

5 Answers5

2

Height and width cannot be applied to span.

You can make it block element or inline-block and then it will accept your height and width.

Read more here: Does height and width not apply to span?

ashfaq.p
  • 5,379
  • 21
  • 35
  • 1
    `inline-block` is more appropriate for spans (since are usually used as contextual wrapping inline elements) - whereas `div` are best suited for a *DIVisor* block element. – Roko C. Buljan May 06 '18 at 17:59
  • 1
    That can be decided as per the need but i have updated the answer to give more clearity:) – ashfaq.p May 06 '18 at 18:00
2

I dont know if you noticed but you have a space between {{sortedUsers$[i].points}} and %; it should be like this with no space

<span style="height: {{sortedUsers$[i].points}}%;">
Abslen Char
  • 3,071
  • 3
  • 15
  • 29
2

Check out ngStyle

Update an HTML element styles.

<span [ngStyle]="{'height': `${sortedUsers$[i].points}%`}"></span>

Your value would look like the following, where each number represent a percent: sortedUsers$[i].points = 100 // Percent

Kyle Pfromer
  • 1,485
  • 1
  • 15
  • 26
1

Use style attribute binding

<span [style.height]="sortedUsers$[i].points">
    <i></i>
   <b>{{sortedUsers$[i].points}}</b>
</span>

Where the value can be this sortedUsers$[i].points = "100%"

John Velasquez
  • 3,421
  • 2
  • 18
  • 23
1

Thank you all for your help! What worked for me was:

 <span class="user" [ngStyle]="{ 'height': getPoints(i)}">
          <i></i>
          <b>{{sortedUsers$[i].points}}</b>
  </span>

with:

getPoints(id) {
return this.sortedUsers$[id].points + '%';}

I am pretty sure some of yours would have worked either, but just letting you all know if someone gets similiar problem later! Such an easy thing got me stuck for hours! Thank you all!

user122222
  • 2,179
  • 4
  • 35
  • 78
  • You could also use a template string for neater syntax, in my personal opinion: `getPoints(id) { return \`${this.sortedUsers$[id].points}%\`;}` – Kyle Pfromer May 06 '18 at 22:00