首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我要将选定的按钮更改为“主”按钮。我将id发送到asp.net核心,并将所选答案的isSelected更改为true

我要将选定的按钮更改为“主”按钮。我将id发送到asp.net核心,并将所选答案的isSelected更改为true
EN

Stack Overflow用户
提问于 2022-07-10 13:55:14
回答 1查看 38关注 0票数 0
代码语言:javascript
复制
<ng-container *ngIf="question.questionType == 4">
  <ng-container *ngFor="let answer of question.answers">
     <button 
         (click)="setanswer(answer.answerId)"   
         [value]="answer.isSelected" 
         class="me-2" 
         name="{{ answer.answerId }}">{{ answer.answerDescription }}
     </button>
  </ng-container>
</ng-container>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-10 16:31:41

您有很多替代方案,假设您定义了这个css类:

代码语言:javascript
复制
.primary {
  background-color: green;
}
代码语言:javascript
复制
<!-- 1) Property Binding -->
<button [class.primary]="answer.isSelected"></button>

<!-- 2) NgClass -->
<button [ngClass]="{ 'primary': answer.isSelected }"></button>

<!-- 3) ngStyle-->
<button [ngStyle]="{ 'background-color': answer.isSelected ? 'green' : 'blue' }"></button>

Sidenote,在您的按钮HTML定义中,您需要为name而不是string interpolation使用属性绑定,所有html属性都可以在Angular中使用property binding

代码语言:javascript
复制
 <button 
   (click)="setanswer(answer.answerId)"   
   [value]="answer.isSelected" 
   class="me-2" 
   [name]="answer.answerId">{{ answer.answerDescription }}
</button>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72929021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档