在Angular 7中有没有办法为模板html属性* key *使用变量?

问题描述 投票:2回答:1

我正在寻找一种将变量用于模板html属性key的方法。例如,

<path [attr.data-**<use-variable-here>**]="edge.id"></path>

Angular 7+中有没有办法做到这一点?

angular angular2-template
1个回答
0
投票

您可以通过@ViewChild执行此操作。

模板:

<path #path></path>

Component:

@ViewChild('path', {static:true}) path: ElementRef;

//... some lifecycle hook | method | callback
ngOnInit() {
  const customVariable = 'my-custom';
  this.path.nativeElement.setAttribute(`data-${customVariable}`, 'my-custom');
}

检查此堆叠闪电战demo

© www.soinside.com 2019 - 2024. All rights reserved.