首页 > 解决方案 > 在Angular 12中更改选定收音机的标签背景

问题描述

在以下示例中检测选择了哪个无线电的最佳方法是什么?

<label for="apple">
    <input id="apple" type="radio" name="fruits" value="apple" class=""/>
    <span>Apple </span>
    <SomeComponent>
</label>
<label for="mango"> 
   <input id="mango" type="radio" name="fruits" value="apple" class=""/>
   <span>Mango </span>
   <AnotherComponent>    
</label>

我希望能够使用 ngClass 为所选标签添加背景。我还可以将该变量用于其他事情(例如在示例中禁用“SomeComponent”或“AnotherComponent”。

标签: angularradio-groupangular12

解决方案


只需在每个收音机的更改事件触发时更新一个变量,然后使用 ngClass 根据变量的值将背景颜色应用于每个标签。

这里有一个例子:Stackblitz


推荐阅读