【发布时间】:2022-02-17 23:46:57
【问题描述】:
所以,我正在创建一个测验应用程序,其中有一个问题滚动条,并且在该滚动条中我有一些按钮,具体取决于问题集的长度。所以我使用 *ngFor 指令创建了这些按钮。现在我想做的是,每当用户选择任何选项(mcq)时,滚动条中的问题按钮应该以下列方式突出显示:
- 如果用户选择任何选项,则将问题按钮颜色更改为绿色
- 如果用户跳过问题,则将问题按钮颜色更改为黄色
问题滚动条的 HTML 代码:
<div id="answer-buttons" class="ques-grid ">
<button #navBarButton *ngFor="let item of items">{{item}}</button>
</div>
我已经尝试通过首先使用 ts 文件中的 ViewChild 访问按钮然后应用一些逻辑来做到这一点,但它不起作用,它只是改变了第一个按钮的颜色
@ViewChild('navBarButton',{ static: false }) navBarButton:ElementRef
//and in some function I've tried this logic
if(this.attemptedQuestionCount[this.currentQuestionIndex]==1){
this.navBarButton.nativeElement.style.backgroundColor = "#228B22"
}
else{
this.navBarButton.nativeElement.style.backgroundColor = "#FCF55F"
}
我怎样才能实现我的目标?
【问题讨论】:
-
ViewChild 仅获取一个唯一元素(第一个),如果您想获得所需的一切,请使用
@ViewChildren-返回元素的 QueryList。
标签: javascript html angular typescript mean-stack