英文:
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.
<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 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:
<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>
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;
}
<!-->
<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>
答案2
得分: 0
以下是您要翻译的内容:
<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>
上面的代码中没有显示绿色进度条,因为用于绑定到 style.width 的 word.correctAnswers 没有带有百分比符号的字符串值,例如 2% 或 25%。我猜它只有一个数字值。所以你可以尝试以下方法,以附加“%”符号:
<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>
或者使用 [style.width.%] 进行绑定:
<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-success" role="progressbar" [style.width]="word.correctAnswers" [attr.aria-valuenow]="word.correctAnswers" aria-valuemin="0" [attr.aria-valuemax]="totalAnswers"></div>
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.
<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>
or bind it using [style.width.%]
<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>
This will now display the greenbar based on width provided.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论