I've created a service to define different colors and now I want to set separate backgrounds for my columns. However, using the <th>
tag doesn't work because both columns immediately get the same color.
Here's my code:
color-variation-service
export class ColorVariationService {
public defaultStyles = {
firstBackgroundColor: '#25a1b1',
secondBackgroundColor: '#c3c0c0',
};
sharedStyleSource = new ReplaySubject<any>(1);
public sharedStyle = this.sharedStyleSource.asObservable();
constructor() { }
newStyle(obj: any) {
this.defaultStyles[obj.name] = obj.value;
console.log('defaultStyles:', this.defaultStyles);
this.sharedStyleSource.next(obj);
}
}
my-component
public myArray = [
{ attribute: 'firstColumn', name: 'Firstname'},
{ attribute: 'secondColumn', name: 'Lastname'},
];
ngAfterViewInit() {
changeFirst('firstColumn');
changeSecond('secondColumn');
}
public changeFirst(attributeToChange: any) {
const displayedColumn = this.displayedColumns.find((dc) => dc.attribute === attributeToChange);
if (displayedColumn && displayedColumn !== null) {
displayedColumn.attribute = this.colorVariationService.defaultStyles.firstBackgroundColor;
}
}
public changeSecond(attributeToChange: any) {
const displayedColumn = this.displayedColumns.find((dc) => dc.attribute === attributeToChange);
if (displayedColumn && displayedColumn !== null) {
displayedColumn.attribute = this.colorVariationService.defaultStyles.secondBackgroundColor;
}
}
This approach doesn't seem to be working as expected. Can you suggest a solution to this issue?