简体   繁体   English

Angular - 无法保持活动 class

[英]Angular - unable to maintain active class

In this app, In each question, I have 4 statements and in those statements, either True or false can be right answers, that is working fine.在这个应用程序中,在每个问题中,我都有 4 个陈述,在这些陈述中,真或假都可以是正确答案,这很好。 But when I click on next statement it removes the class from previously selected element.但是当我单击下一条语句时,它会从先前选择的元素中删除 class 。 I want to select either true or false from any statement and it should maintain its active state when I click on next statement's button.我想 select 从任何语句中判断为真或假,当我单击下一个语句的按钮时,它应该保持其活动 state 。 What is the way I can handle that?我有什么办法可以处理? I'm using IDs so I can't think of any workaround.我正在使用 ID,所以我想不出任何解决方法。 Please any help or suggestions..请任何帮助或建议..

Working Stackblitz Demo: https://stackblitz.com/edit/angular-ivy-4yczgp工作 Stackblitz 演示: https://stackblitz.com/edit/angular-ivy-4yczgp

<div class="statement-row"
                               *ngFor="let item of qblock.options;let j = index"
                               [class.correct-answer]="item.id == id && right"
                               [class.wrong-answer]="item.id == id && wrong"
                               >
                               <!-- [ngClass]="{'wrong-answer': item.selectedIndex === j,'correct-answer':item.customIndex === j}" -->
                            <div class="statement-question">
                              <div class="qitem-text">
                                <div class="qitem-textbox">
                                  <p>{{item.statement}}</p>
                                </div>
                              </div>
                              <div class="ccq">
                                <div class="qitem" [class.green-class-right]="highlight_false_green && item.id == id" [class.red-class-wrong]="highlight_false_red  && item.id == id"
                                        (click)="clickFalse(item)">
                                  <i class="qitembox qclose-icon"></i>
                                </div>
                                <div class="qitem" [class.green-class-right]="highlight_true_green  && item.id == id" [class.red-class-wrong]="highlight_true_red  && item.id == id"
                                (click)="clickTrue(item)">

                                  <i class="qitembox qtick-icon"></i>
                                </div>
                              </div>
                            </div>
                            <div class="correct-statement">
                              <span class="true-statement">This statment is True</span>
                              <span class="false-statement">This statment is False</span>
                              <em class="wrong-answer-description">
                                {{item.explanation}}
                              </em>
                              <em class="correct-answer-description">
                                {{item.explanation}}
                              </em>
                            </div>

                          </div>

I reworked the component, so I think it works as you want.我重新设计了组件,所以我认为它可以按您的意愿工作。 You can find the code here: https://stackblitz.com/edit/angular-ivy-pctp7z你可以在这里找到代码: https://stackblitz.com/edit/angular-ivy-pctp7z

If you have any further questions, please feel free to ask them.如果您还有其他问题,请随时提出。 It's a bit difficult to describe all the code changes.描述所有代码更改有点困难。 I hope it helps!我希望它有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM