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。我理解文件错误,不客气!不要忘记选择答案作为“接受答案”来完成此问题。