angular - Angular 6 - 选择名称时显示动态div
问题描述
我有一个数组,数组内容显示为员工姓名的复选框列表。我想,当我选择一个复选框(选择员工姓名)时,它将显示一个 DIV,其中包含所选员工姓名和相关员工的文本区域(用于评论)。该 DIV 将为列表中的每个选定名称动态显示。请指导我如何达到所需的结果。
解决方案
我会这样做:
在您的 component.ts 中:
selectedEmployees: string[];
toggleEmployee(empl: string): void {
const index = this.selectedEmployees.indexOf(empl)
if(index > -1) {
this.selectedEmployees.push(empl);
} else {
this.selectedEmployees.splice(index, 1);
}
}
在您的 html 中:
<input type="checkbox" (click)="toggleEmployees('James')" value="James">
<input type="checkbox" (click)="toggleEmployees('Charles')" value="Charles">
<div *ngIf="let empl of selectedEmployees">
{{ empl }}
<div>
我希望这回答了你的问题
推荐阅读
- python - 在 python3 加载的 unix 环境中运行 python2 脚本
- c++ - 将 unix 时间戳转换为人类可读的日期
- regex - Notepad++ 正则表达式反向引用似乎不起作用
- asp.net-core - 如何使用 EFCore Code First 迁移指定复合主键
- sql - 是否可以在更新一列或多列时复制表中的所有值
- .net - 如何在使用 LINQ to SQL 时找到给定字段的服务器数据类型
- python - 控制台中的输入文本被程序的输出分成多行
- php - 当您发布大原始数据时,PHP 是否正在写入磁盘?
- sql - SQL 循环遍历年份,但按会计年度,而不是日历年
- statistics - 寻找一种绘制置信水平等高线的详细方法