0

In Polymer we have this concept where we add specific properties to certain components, and then style that component with associated attribute. (This way, it differentiates them from the same component that doesnt have that attribute.

For example

Polymer

<osb-retailer-details overlay></osb-retailer-details>
<osb-retailer-details></osb-retailer-details>

And then to style <osb-retailer-details> component with overlay attribute, we do the following:

:host {
  box-sizing: border-box;
  display: block;
  margin: 0 0 15px;
  
  &[overlay] {
    margin-bottom: 0;
  }
}

Now my question

In angular4, can we do the same thing? Where we pass attribute to component, and style it, just like above?

Thanks

6
  • Yes, it basically works the same way: plnkr.co/edit/OZv3AL1YsHYQHedXTjBr?p=preview Commented Aug 15, 2017 at 11:16
  • I see. So judging by that, this means that If I'm passing an object to a child component (e.g <osb-retailer-details [prevCard] ="person"></ osb-retailer-details>). I can style the child component by doing :host[prevCard]{...}? @JBNizet Commented Aug 15, 2017 at 11:25
  • Well, yes. Why don't you test it? Commented Aug 15, 2017 at 11:26
  • @JBNizet It didnt work :( not sure why, but it made sense so much. It should've worked Commented Aug 15, 2017 at 11:54
  • actually, it worked. Lmao, not sure why. Just refreshed the page @JBNizet Commented Aug 15, 2017 at 11:56

2 Answers 2

1

Yes, it basically works the same way. Here's an example.

component definition:

@Component({
  selector: 'osb-retailer-details',
  template: '<div>hello world</div>',
  styleUrls: ['src/retailer-details.css']
})
export class RetailerDetails {

}

src/retailer-details.css file:

:host {
  display: block;
  background-color: yellow;
}

:host[overlay] {
  border: 4px solid red;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you bud. Really appreciate your help :)
0
<osb-retailer-details [attr.overlay]="'overlay'"></osb-retailer-details>

and then the rest is the same

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.