0

Is there a possibility to do something like this:

<span data-ng-style="vm.myFlag == true ? 'background-color:{{myObject.color}};padding:2px;border-radius:2px;' : ''">

This if - else does not work. ngClass does not work because the color is a property of my object(s).

quma
  • 5,233
  • 26
  • 80
  • 146

3 Answers3

2

https://docs.angularjs.org/api/ng/directive/ngStyle

Basic Example of ng-style :

<span ng-style="{'background-color':'blue'}">Sample Text</span>

Using ternary operator to make styling conditional on vm.myFlag:

<span data-ng-style="(vm.myFlag == true) ? {'background-color':
 myObject.color, 'padding': '2px', 'border-radius': '2px'}
 : {}">
FeRD
  • 1,699
  • 15
  • 24
Aks1357
  • 1,062
  • 1
  • 9
  • 19
0
<span data-ng-style="vm.myFlag && {'background-color':myObject.color}">
0

You could do this:

<span data-ng-style="vm.geMyStyle()">

and in your controller:

vm.getMyStyle= func(){

    let style = vm.myFlag == true ? 'background-color:' 
    +myObject.color+';padding:2px;border-radius:2px;' : '';

    return style;
}

but you also should check if there is the color-property set.

Nikolaus
  • 1,859
  • 1
  • 10
  • 16