angular - 在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”。
解决方案
只需在每个收音机的更改事件触发时更新一个变量,然后使用 ngClass 根据变量的值将背景颜色应用于每个标签。
这里有一个例子:Stackblitz
推荐阅读
- json - Flutter:如何使用 GetX 包从 List 生成 json 文件
- php - Laravel - 仅对某些特定值进行分组
- javascript - 反应:无法使用 API 读取未定义的属性“地图”
- java - 启动 Spring Boot/Spring Security 应用程序时,如何修复“无法将 JdbcUserDetailsManagerConfigurer 应用于已构建的对象”错误消息?
- firebase - 我如何专门查询firebase实时数据库中的字段
- php - 路线 [posts.index] 未定义
- node.js - 护照身份验证和重定向遇到问题
- python - 为什么将垂直列表转换为字符串会水平显示其所有元素?
- c# - ADX:无法从 .NET sdk 提取带有 JSON 映射的数据
- angular - 如何映射服务器响应以获取错误和成功