2

Is the class attribute useful in angular 2 for a customized component? For example:

<app-my-first-component class='darkBackground'></app-my-first-component>

It looks like the class does not affect the component.

3
  • what are you trying to do which is not being reflected? Commented Mar 6, 2017 at 19:21
  • 1
    class attribute any where inside HTML is used to add a css class. Does your question makes sense? Commented Mar 6, 2017 at 19:25
  • I think he/she wants to pass the class attribute to child component. If that's the case, you can define global css style in the file src/styles.css. Otherwise, parent and child don't have same scope, so the css styles in parent are not accessible in child Commented Mar 6, 2017 at 19:32

2 Answers 2

1

You can address the class by adding a style to app-my-first-component like

@Component({
  ...,
 styles: [`
  :host(.darkBackground) {
    color: white;
    background-color: darkgrey;
  }`]
})
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, you can use class attribute. And, there is another way to do the same, using host attribute.

@Component({
selector: 'app-my-first-component',
host: {'class':'darkBackground'}
})

Comments

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.