anandaravindan
anandaravindan

Reputation: 2541

Angular js applying Gradients with ng style

Unable to set gradients using angular js ng-style directive.. able to set normal color by using below

<span ng-style="{'background-color': slidercolor}">works</span>

But for gradient it doesnt work

Below is the jsfiddle for the same.

<span ng-style="{'background-color':'-webkit-gradient(linear, left top, right top, color-    stop(0%,'slidercolor'), color-stop(100%,rgba(125,185,232,0)))'}">didnt work</span>

http://jsfiddle.net/sT8ar/1/

Also please let me know which should be enclosed in quotes and which should not..

Upvotes: 3

Views: 8904

Answers (2)

Vladimir Denisov
Vladimir Denisov

Reputation: 117

I hope this helps you.

controller

private setStyles() {
  const rgbaGradientStart = hexToRgba(this.hexGradientStart, 1);
  const rgbaGradientEnd = hexToRgba(this.hexGradientEnd, 0.1);
  const gradientParams = `left, ${rgbaGradientStart} 0%, ${rgbaGradientEnd} 50%`;

  this.gradientStyles = {
    'background-image': `-webkit-linear-gradient(${gradientParams})`,
    // 'background-image': `-moz-linear-gradient(${gradientParams})`,
    // 'background-image': `-o-linear-gradient(${gradientParams})`,
    // 'background-image': `-ms-linear-gradient(${gradientParams})`,
    // 'background-image': `linear-gradient(${gradientParams})`,
  }
}

template

<div
  class="super-gradient"
  [ngStyle]="gradientStyles"
></div>

P.S.: you may also be interested in this
https://github.com/angular/angular/issues/11362#issuecomment-244816438

Upvotes: 1

Pavlo
Pavlo

Reputation: 44889

You got two problems here:

  1. -webkit-gradient() produces <image>, not <color>, it shoud be used as background-image.
  2. In Angular expression concatenation is done with a + (plus sign).

So the correct syntax is (demo):

<span ng-style="{'background-image':'-webkit-gradient(linear, left top, right top,   color-stop(0%,'+ slidercolor+'), color-stop(100%,rgba(125,185,232,0)))'}">
  Works now too.
</span>

Upvotes: 9

Related Questions