javascript - Angular 6 使用 ngFor 中不同列的 Renderer2 更改样式
问题描述
我已经使用 ngFor 向表中添加了列。当用户单击<td>
它时,它会打开一个对话框并返回一些值。根据选定的值,它<td>
使用 Renderer2 更改特定的背景颜色。基于返回的值,我也想更改其他颜色的颜色<td>
。返回值具有我想要进行更改item
的 s 对象。<td>
我怎样才能做到这一点?
<td
*ngFor="let item of items; index as i"
(click)="openDialog(someVal)"
#someVal
>
组件.ts
openDialog(someVal): void {
// Some Conditions .................
this.rd.setStyle(someVar, 'background-color', this.colorCode);
});
}
解决方案
您可以使用 Directive :您可以定义自己的指令来将行为附加到
<td>
DOM 中的元素。
<td [dialog]="someVal"
*ngFor="let item of items; index as i"
>
班级
import { fromEvent } from 'rxjs';
@Directive({
selector: '[dialog]'
})
export class DialogDirective{
public action: ElementRef ;
constructor( elementRef: ElementRef<HTMLElement>) {
this.action = elementRef ;
}
@Input() set dialog( someVal : any ) {
// Some Conditions ...
// Subscription
fromEvent(this.action.nativeElement, 'click').pipe(
.....
}
推荐阅读
- dictionary - LiteDB - case sensitivity when storing Dictionary()
- android - Turn from the other side -landscape
- linux - Other than the file extension and execution, how to determine if a file is a PowerShell script vs a Bash script?
- javascript - How to reduce build time for Angular 11.1.1?
- javascript - 如何在 reactjs-form-builder 字段中预先设置数据?
- c++ - C++ 中一系列模板参数的显式模板实例化
- javascript - 如何一次触发所有输入的onchange事件?
- regex - 正则表达式从特定表达式中捕获多行,直到 Perl 中的第一个空行
- apache-spark - 我怎样才能杀死卡住的工作而不杀死 Spark 中的应用程序?
- java - 如何让paintComponent 使用我的代码?