Dynamically change the style of an undeclared html child component in Angular - TagMerge
4Dynamically change the style of an undeclared html child component in AngularDynamically change the style of an undeclared html child component in Angular

Dynamically change the style of an undeclared html child component in Angular

Asked 1 years ago
2
4 answers

Hello Vincent Thomason,

From what I Understood, you are trying to style the div that is nested inside the p-card, that you don't have access to in your component HTML.

The only solution that I see is to give your p-card an id. Then use that id in querySelector and select the div inside ('myCardId>.p-card').

Here is a working stackblitz with what you want to accomplish:

https://stackblitz.com/edit/angular-ivy-nxjpwe?file=src/app/app.component.ts

Source: link

0

I have following markup
<tr *ngFor='let activity of pagedWorkflowActivities' style="background-color:{{activity.status == 'Pending' ? 'red' : 'green'}}">
.
.
.
.
</tr>
As it says, if activity.status field is pending then make background color red otherwise green. But it doesn't work. After inspecting I found it renders it like
<tr ng-reflect-style="unsafe">
[style.background-color]="activity.status == 'Pending' ? 'red' : 'green'"
or
[ngStyle]="{'backgroundColor': activity.status == 'Pending' ? 'red' : 'green' }"
Try this one:
[ngStyle]="{'border': user?.keyResults.percentage > 50 ? '3px solid green' : '3px solid red' }"

Source: link

0

parent.template.html
<div class="parent__container">
  <app-child></app-child>
</div>
parent.component.css
.parent__container {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: peru;
  width: 300px;
  height: 300px;
}
child.template.html
<div class="child__container"></div>
child.component.css
.child__container {
  background-color: green;
  width: 150px;
  height: 150px;
}
parent.component.css
.parent__container {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: peru;
  width: 300px;
  height: 300px;
}

.parent__container.alert .child__container {
  background-color: darkred;
}

Source: link

0

Both host binding and ngStyles provide great ways to dynamically update styles. But what happens if Angular deems our style value unsafe, and we get this warning?:
WARNING: sanitizing unsafe style value {some value}

Source: link

Recent Questions on html

    Programming Languages