Angular2向元素添加属性 [英] Angular2 adding attribute to element

查看:56
本文介绍了Angular2向元素添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在创建元素时将其添加到元素?

How do I add an attribute to an element when it is created ?

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

I tried the following Dashboard component:

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','');
  }
}

我想在创建DashboardComponent时向div添加 re-component 属性.然后,我使用 re-component 设置创建的div的样式.但是,我收到以下错误: http://pastebin.com/up35zmUv

I would like to add re-component attribute to the div when the DashboardComponent is being created. I then use re-component to style the created div. However I get the following error: http://pastebin.com/up35zmUv

任何想法

最诚挚的问候

推荐答案

尝试一下:

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

这篇关于Angular2向元素添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆