I am attempting to dynamically change the gradient background of some text in Angular.
My current code is as follows:
home.component.html
<div class="name" [style.background]="gradient"
[style.-webkit-background-clip]="'text'"
[style.background-clip]="'text'">Text Here</div>
home.component.sass
.name
color: transparent
font-family: Tahoma, sans-serif
background: #FA8BFF
background-clip: text
-webkit-background-clip: text
home.component.ts
...
gradient : string = "linear-gradient(270deg, #FA8BFF 0%, #2BD2FF 42%, #2BFF88 90%)";
ngOnInit(): void {
addEventListener("mousemove", (event) => {
var angle = this.mouseAngle(event.clientX, event.clientY);
this.gradient = `linear-gradient(${angle}deg, #FA8BFF 0%, #2BD2FF 42%, #2BFF88 90%)`
})
}
...
This works as expected when the page is loaded, but the instant that I trigger the mousemove event, the text turns into a square for the rest of the time the page is active.
How do I properly apply a dynamic gradient background to my Angular element?
2
Answers
@HostListener
. which is preferred by angular docs.stackblitz
Renderer2
. It help us to manipulate the DOM. stackblitzRxJS
, I love this one stackblitzHope it help you.
Use (see the "text" before linear-gradient)
And