Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rgba value is not updated in ng-style

I have a div which has an ng-style attribute with an rgba value that uses a $scope variable for the alpha value:

ng-style="{'background-color': 'rgba(255,0,0,{{ alphaValue / 100}})'}"

I have an input slider which changes the value of alphaValue. I can see the change of the value using the console.log and the rgba value is being updated in the debugger in the html. It is just not being applied in the view.

Any suggestions?

like image 498
GusGus Avatar asked Jan 08 '23 21:01

GusGus


2 Answers

It would simply look like below

ng-style="{'background-color': 'rgba(255,0,0,'+ (alphaValue/ 100) +')'}"
like image 165
Pankaj Parkar Avatar answered Jan 17 '23 20:01

Pankaj Parkar


Create a controller method get the style object. Its cleaner approach.

HTML

ng-style="ctrl.getElementStyle(alphaValue)"

CONTROLLER

 ctrl.getElementStyle = function (alphaValue) {
        var alpha = alphaValue / 100;
        return {'background': 'rgba(255,0,0,' + alpha +')'};
  };
like image 29
Liam Avatar answered Jan 17 '23 19:01

Liam