1

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

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

Any ideas

Best regards

1 Answer 1

2

Try this:

@Component({
  selector: '[re-dashboard]',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.scss'],
  host: {
      "re-component": "",
  }
})
export class DashboardComponent {}
Sign up to request clarification or add additional context in comments.

3 Comments

This works by adding a class. Good. However I am now more interested in a way to do this with code and to add an attribute ?
EDIT - this is what I want. Sry. I have understood the documentation wrong
You`r welcome! Don´t forget to pick the answer as "accepted answer" to complete this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.