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

标签 angular angular2-template

我正在寻找一种将变量用于模板 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

关于angular - 有没有办法在 Angular 7 中使用模板 html 属性 *key* 的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59221644/

相关文章:

angular - 如何更改 Ionic 4 的 ionic 范围引脚字体和格式?

Angular2,如何获取当前的事件组件选择器以将其用作全局类以进行 css 样式设置

angular - 我无法连接到Google Cloud Platform中的 Elasticsearch

javascript - Angular 6+ 应用程序的运行时配置

Angular 2 阅读更多指令

组件模板中的 Angular 2 路由器导出

angular - 如何在 Angular 2的运行时动态更改templateUrl值

angular - 在 Angular 2 View 中显示可观察的嵌套数据

Angular2 观察对象/数组变化(Angular2 final >= 2.1.1)

angular - ExpressionChangedAfterItHasBeenCheckedError 未定义输入参数