简体   繁体   English

使用什么css属性来对选中的单选按钮设置角度样式?

[英]What css property to use to style a checked radio button in angular?

I have a radio button group like this in my angular 4.1.3 app: 我的Angular 4.1.3应用程序中有一个单选按钮组:

<form #f="ngForm">
  <label class="btn btn-success">
    <input type="radio" value="beef" name="food" [(ngModel)]="myFood"> Beef
  </label>
  <label class="btn btn-success">
   <input type="radio" value="lamb" name="food" [(ngModel)]="myFood"> Lamb
  </label>
  <label class="btn btn-success">
   <input type="radio" value="fish" name="food" [(ngModel)]="myFood"> Fish
  </label>
</form>

This works well. 这很好。 However, I would like to style the label (including the button) when the corresponding radio button is checked. 但是,我想在选中相应的单选按钮时设置标签(包括按钮)的样式。

I can't see any useful attribute: 我看不到任何有用的属性:

在此处输入图片说明

(first one is checked) (检查第一个)

I tired the obvious: 我很明显:

input[type=radio]:checked {
      background: red;
}

That doesn't work... Any ideas how I could select the checked button in CSS? 那行不通...关于如何在CSS中选择选中按钮的任何想法?

Unfortunately you cannot style parent using child :checked pseudo-selector. 不幸的是,您不能使用child :checked伪选择器设置父样式。 To get this working i recommend use model value, eg: 为了使此工作正常,我建议使用模型值,例如:

<form #f="ngForm">
  <label class="btn btn-success" [ngClass]="{selected: myFood==='beef'">
    <input type="radio" value="beef" name="food" [(ngModel)]="myFood"> Beef
  </label>
  <label class="btn btn-success" [ngClass]="{selected: myFood==='lamb'">
   <input type="radio" value="lamb" name="food" [(ngModel)]="myFood"> Lamb
  </label>
  <label class="btn btn-success" [ngClass]="{selected: myFood==='fish'">
   <input type="radio" value="fish" name="food" [(ngModel)]="myFood"> Fish
  </label>
</form>

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

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