有没有办法在 Angular 7 中为模板 html 属性 *key* 使用变量?
Is there a way to use a variable for template html property *key* in Angular 7?
我正在寻找一种为模板使用变量的方法 html 属性 key。例如,
<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 demo
我正在寻找一种为模板使用变量的方法 html 属性 key。例如,
<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 demo