我正在寻找一种使用变量作为模板html属性键的方法。例如,
<path [attr.data-**<use-variable-here>**]="edge.id"></path>
有没有办法在Angular 7+中做到这一点?
您可以通过@ViewChild进行此操作。
模板:
<path #path></path>
零件:
@ViewChild('path', {static:true}) path: ElementRef;
//... some lifecycle hook | method | callback
ngOnInit() {
const customVariable = 'my-custom';
this.path.nativeElement.setAttribute(`data-${customVariable}`, 'my-custom');
}
查看此stackblitz演示
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句