In angular material, we can pass colors to angular material components as attribute, for example
<button mat-raised-button color="primary">Example</button>
I'm using multiple angular material themes, which changes dynamically.
Required Behaviour: Changing color of my custom components dynamically with theme change.
I want something like this:
<app-footer color="primary"></app-footer>
How to get color in my custom component?
What I tried
I had a input property in my component
@Input() color;
and tried to pass color in component
<app-footer color="primary"></app-footer>
But it didn't worked for me.
How to achieve this?
==UPDATE==
Explanation: Below lines of code are from button.ts file of angular material from its github repository
/**
* Material design button.
*/
@Component({
moduleId: module.id,
selector: `button[mat-button], button[mat-raised-button], button[mat-icon-button],
button[mat-fab], button[mat-mini-fab], button[mat-stroked-button],
button[mat-flat-button]`,
exportAs: 'matButton',
host: {
'[attr.disabled]': 'disabled || null',
'[class._mat-animation-noopable]': '_animationMode === "NoopAnimations"',
},
templateUrl: 'button.html',
styleUrls: ['button.css'],
inputs: ['disabled', 'disableRipple', 'color'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
Link to file: Button.ts
Here you can see, it takes color as input. Similarly, I have also taken color as input in my footer component. Now with <app-footer color="primary"></app-footer> I got primary string as input in my footer component.
How to get color hex from this to set background color of my footer component. Please note, primary color changes dynamically with theme change, therefore, I cannot hard-code color.
<app-footer [color]="color"></app-footer>(notice the[brackets]), it will evaluate the expression instead of just using the string you give to it. - ShamPooSham