Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2向元素添加属性_Angular - Fatal编程技术网

Angular2向元素添加属性

Angular2向元素添加属性,angular,Angular,创建元素时,如何向元素添加属性 我尝试了以下仪表板组件: import { Component, ElementRef, Renderer } from '@angular/core'; @Component({ selector: '[re-dashboard]', templateUrl: './dashboard.component.html', styleUrls: ['./dashboard.component.scss'] }) export class Dashbo

创建元素时,如何向元素添加属性

我尝试了以下仪表板组件:

import { Component, ElementRef, Renderer  } from '@angular/core';

@Component({
  selector: '[re-dashboard]',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent {

  constructor (private _el: ElementRef, private _renderer: Renderer) {
    _renderer.setElementAttribute(_el, 're-component','');
  }
}
在创建仪表板组件时,我想向div添加
re component
属性。然后,我使用
re component
对创建的div进行样式设置。但是,我得到以下错误:

有什么想法吗

致以最诚挚的问候

试试这个:

@Component({
  selector: '[re-dashboard]',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss'],
  host: {
      "re-component": "",
  }
})
export class DashboardComponent {}

这通过添加一个类来实现。好。然而,我现在更感兴趣的是如何用代码来实现这一点,并添加一个属性?编辑-这就是我想要的。Sry。我理解文件错误,不客气!不要忘记选择答案作为“接受答案”来完成此问题。