如何在Angular、Bootstrap中动态更改进度条

huangapple go评论45阅读模式
英文:

How to dynamically change progress bar in Angular, Boostrap

问题

Got a Word object (for language vocabulary training app), which contains the number of correct and wrong answers. Trying to display a progress bar which will have info about these answers.

<div class="progress">
  <div class="progress-bar progress-bar-striped progress-bar-animated bg-success" role="progressbar" [style.width]="word.correctAnswers" [attr.aria-valuenow]="word.correctAnswers" aria-valuemin="0" [attr.aria-valuemax]="totalAnswers"></div>
  <div class="progress-bar progress-bar-striped progress-bar-animated bg-danger" role="progressbar" style="width: 20%" attr.aria-valuenow="{{word.wrongAnswers}}" aria-valuemin="0" attr.aria-valuemax="{{totalAnswers}}"></div>
</div>

the TypeScript code:

@Input()
word!: Word;

totalAnswers!: number;

ngOnInit(): void {
  this.totalAnswers = this.word.correctAnswers + this.word.wrongAnswers;
}

Got no luck so far. Been trying different ways according to info I found, as you can see above. No green bar displayed at all at the moment, with the red bar staying at a fixed 20 percent. When I try to change its style.width, it disappears too.

英文:

Got a Word object (for language vocabulary training app ), which contains number of correct and wrong answers. Trying to display a progress bar which will have info about this answers.

&lt;div class=&quot;progress&quot;&gt;

      &lt;div class=&quot;progress-bar progress-bar-striped progress-bar-animated bg-success&quot; role=&quot;progressbar&quot; [style.width]=&quot;word.correctAnswers&quot; [attr.aria-valuenow]=&quot;word.correctAnswers&quot; aria-valuemin=&quot;0&quot; [attr.aria-valuemax]=&quot;totalAnswers&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;progress-bar progress-bar-striped progress-bar-animated bg-danger&quot; role=&quot;progressbar&quot; style=&quot;width: 20%&quot; attr.aria-valuenow=&quot;{{word.wrongAnswers}}&quot; aria-valuemin=&quot;0&quot; attr.aria-valuemax=&quot;{{totalAnswers}}&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;

the ts:

@Input()
  word!: Word;

  totalAnswers!: number;

  ngOnInit(): void {
    this.totalAnswers = this.word.correctAnswers + this.word.wrongAnswers;
  }

Got no luck so far. Been trying different ways according to info i found, as you can see above. No green bar displayed at all at the moment, with red bar staying at fixed 20 percent. When i try to change its style.width, it disappears too.

答案1

得分: 2

计算百分比(最好在组件中创建单独的变量),然后使用 [style.width.%] 表示法将其设置为宽度。

尝试这样做:

<div class="progress">
  <div class="progress-bar progress-bar-striped progress-bar-animated bg-success" role="progressbar" [style.width.%]="(100 * word.correctAnswers)/totalAnswers" [attr.aria-valuenow]="word.correctAnswers" aria-valuemin="0" [attr.aria-valuemax]="totalAnswers"></div>
  <div class="progress-bar progress-bar-striped progress-bar-animated bg-danger" role="progressbar" [style.width.%]="(100 * word.wrongAnswers)/totalAnswers" attr.aria-valuenow="{{word.wrongAnswers}}" aria-valuemin="0" attr.aria-valuemax="{{totalAnswers}}"></div>
</div>

或者在组件中使用变量:

totalAnswers!: number;
correctPercentage!: number;
wrongPercentage!: number;

ngOnInit(): void {
  this.totalAnswers = this.word.correctAnswers + this.word.wrongAnswers;
  this.correctPercentage = (100 * this.word.correctAnswers)/this.totalAnswers;
  this.wrongPercentage = (100 * this.word.wrongAnswers)/this.totalAnswers;
}
<div class="progress">
  <div class="progress-bar progress-bar-striped progress-bar-animated bg-success" role="progressbar" [style.width.%]="correctPercentage" [attr.aria-valuenow]="word.correctAnswers" aria-valuemin="0" [attr.aria-valuemax]="totalAnswers"></div>
  <div class="progress-bar progress-bar-striped progress-bar-animated bg-danger" role="progressbar" [style.width.%]="wrongPercentage" attr.aria-valuenow="{{word.wrongAnswers}}" aria-valuemin="0" attr.aria-valuemax="{{totalAnswers}}"></div>
</div>
英文:

Calculate percentage (preferably create separate variable in the component), and then set it as width with [style.width.%] notation.

Try this:

&lt;div class=&quot;progress&quot;&gt;
&lt;div class=&quot;progress-bar progress-bar-striped progress-bar-animated bg-success&quot; role=&quot;progressbar&quot; [style.width.%]=&quot;(100 * word.correctAnswers)/totalAnswers&quot; [attr.aria-valuenow]=&quot;word.correctAnswers&quot; aria-valuemin=&quot;0&quot; [attr.aria-valuemax]=&quot;totalAnswers&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;progress-bar progress-bar-striped progress-bar-animated bg-danger&quot; role=&quot;progressbar&quot; [style.width.%]=&quot;(100 * word.wrongAnswers)/totalAnswers&quot; attr.aria-valuenow=&quot;{{word.wrongAnswers}}&quot; aria-valuemin=&quot;0&quot; attr.aria-valuemax=&quot;{{totalAnswers}}&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

or with variables in the component:

  totalAnswers!: number;
  correctPercentage!: number;
  wrongPercentage!: number;

  ngOnInit(): void {
    this.totalAnswers = this.word.correctAnswers + this.word.wrongAnswers;
    this.correctPercentage = (100 * this.word.correctAnswers)/this.totalAnswers;
    this.wrongPercentage = (100 * this.word.wrongAnswers)/this.totalAnswers;

  }

&lt;!--&gt;

  &lt;div class=&quot;progress&quot;&gt;
&lt;div class=&quot;progress-bar progress-bar-striped progress-bar-animated bg-success&quot; role=&quot;progressbar&quot; [style.width.%]=&quot;correctPercentage&quot; [attr.aria-valuenow]=&quot;word.correctAnswers&quot; aria-valuemin=&quot;0&quot; [attr.aria-valuemax]=&quot;totalAnswers&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;progress-bar progress-bar-striped progress-bar-animated bg-danger&quot; role=&quot;progressbar&quot; [style.width.%]=&quot;wrongPercentage&quot; attr.aria-valuenow=&quot;{{word.wrongAnswers}}&quot; aria-valuemin=&quot;0&quot; attr.aria-valuemax=&quot;{{totalAnswers}}&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

答案2

得分: 0

以下是您要翻译的内容:

&lt;div class=&quot;progress-bar progress-bar-striped progress-bar-animated bg-success&quot; role=&quot;progressbar&quot; [style.width]=&quot;word.correctAnswers&quot; [attr.aria-valuenow]=&quot;word.correctAnswers&quot; aria-valuemin=&quot;0&quot; [attr.aria-valuemax]=&quot;totalAnswers&quot;&gt;&lt;/div&gt;

上面的代码中没有显示绿色进度条,因为用于绑定到 style.width 的 word.correctAnswers 没有带有百分比符号的字符串值,例如 2% 或 25%。我猜它只有一个数字值。所以你可以尝试以下方法,以附加“%”符号:

&lt;div class=&quot;progress-bar progress-bar-striped progress-bar-animated bg-success&quot; role=&quot;progressbar&quot;
      [style.width]=&quot;word.correctAnswers + &#39;%&#39;&quot; [attr.aria-valuenow]=&quot;word.correctAnswers&quot; aria-valuemin=&quot;0&quot;
      [attr.aria-valuemax]=&quot;totalAnswers&quot;&gt;&lt;/div&gt;

或者使用 [style.width.%] 进行绑定:

&lt;div class=&quot;progress-bar progress-bar-striped progress-bar-animated bg-success&quot; role=&quot;progressbar&quot;
      [style.width.%]=&quot;word.correctAnswers&quot; [attr.aria-valuenow]=&quot;word.correctAnswers&quot; aria-valuemin=&quot;0&quot;
      [attr.aria-valuemax]=&quot;totalAnswers&quot;&gt;&lt;/div&gt;

现在将根据提供的宽度显示绿色进度条。
英文:
&lt;div class=&quot;progress-bar progress-bar-striped progress-bar-animated bg-success&quot; role=&quot;progressbar&quot; [style.width]=&quot;word.correctAnswers&quot; [attr.aria-valuenow]=&quot;word.correctAnswers&quot; aria-valuemin=&quot;0&quot; [attr.aria-valuemax]=&quot;totalAnswers&quot;&gt;&lt;/div&gt;

The green bar is not displayed on above code because the word.correctAnswers which was used to bind to style.width does not have a string value with the percentage sign like 2% or 25%. Im guessing it only has the number on it. So you can maybe do below to append the % sign.

&lt;div class=&quot;progress-bar progress-bar-striped progress-bar-animated bg-success&quot; role=&quot;progressbar&quot;
      [style.width]=&quot;word.correctAnswers + &#39;%&#39;&quot; [attr.aria-valuenow]=&quot;word.correctAnswers&quot; aria-valuemin=&quot;0&quot;
      [attr.aria-valuemax]=&quot;totalAnswers&quot;&gt;&lt;/div&gt;

or bind it using [style.width.%]

&lt;div class=&quot;progress-bar progress-bar-striped progress-bar-animated bg-success&quot; role=&quot;progressbar&quot;
      [style.width.%]=&quot;word.correctAnswers&quot; [attr.aria-valuenow]=&quot;word.correctAnswers&quot; aria-valuemin=&quot;0&quot;
      [attr.aria-valuemax]=&quot;totalAnswers&quot;&gt;&lt;/div&gt;

This will now display the greenbar based on width provided.

huangapple
  • 本文由 发表于 2023年4月4日 15:50:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定