Reputation: 957
I am using ngPrime components and if i style them styles are not applying on dashboard.component.sass file but they apply when i use the global style.sass file.
dashboard.component.html file
<p-dropdown [options]="reports" styleClass="report-dropdown">
<ng-template let-item pTemplate="selectedItem">
<i class="fas fa-th" style="fill: white;"></i>
<span style="vertical-align:middle">
{{item.label}}</span>
</ng-template>
</p-dropdown>
dashboard.component.scss and global style.scss file
.report-dropdown {
.ui-dropdown-label {
background-color: $secondary;
color: white;
}
.ui-dropdown-trigger {
color: white;
background-color: $secondary;
border: none;
}
}
Upvotes: 4
Views: 3489
Reputation: 786
By default, Angular component styles are scoped to affect the component's view. This means that the styles you write will affect all the elements in your component template. They will not affect elements that are children of other components within your template. You can read more about view encapsulation in the Angular documentation.
If your component has view encapsulation turned on (default), your component styles will only affect the top level children in your template. HTML elements belonging to child components cannot be targeted by your component styles unless you do one of the following:
Upvotes: -1
Reputation: 34435
If you want to set the style in your component, you just need to use ng-deep
before the rule you want to apply.
https://angular.io/guide/component-styles#deprecated-deep--and-ng-deep
It is indeed deprecated, but there is no replacement so far so you may as well use it for now
dashboard.component.scss
::ng-deep .report-dropdown {
.ui-dropdown-label {
background-color: $secondary;
color: white;
}
.ui-dropdown-trigger {
color: white;
background-color: $secondary;
border: none;
}
}
I don't know primeng, but I forked an old stackblitz showing color change (dropdown does not open on example though)
https://stackblitz.com/edit/primeng-bootstrap-ylpzwd?file=app/app.component.scss
Other solution
The other solution is to set the style in your global style sheet. This will work provided that your CSS rules are more specific that the ones applied by default by ngPrime
Upvotes: 6