0

I have a dynamic style model as a string, which I want to apply to a html element. Unfortunately ng-style works with objects.

How can I make this work with a simple string?

item.trStyle is something like "background-color:#B5EFF2;padding:10px;text-align:center;"

<td style="{{item.trStyle}}">

</td>
4
  • 2
    Use ng-style, not style docs.angularjs.org/api/ng/directive/ngStyle Commented Aug 25, 2014 at 15:41
  • I tried with ng-style and not working. Expecting an object. Commented Aug 25, 2014 at 15:43
  • Right..so put your css into an object {'background-color': '#B5EFF2','padding':'10px','text-align':'center'} Commented Aug 25, 2014 at 15:47
  • so does this style change depending on a circumstance or is this just a static style? Commented Aug 25, 2014 at 16:14

1 Answer 1

1

You may need to use ng-class with several different classes that builds the style you want. For example..

<td ng-class="{blueBG: shouldBeBlue, tenPadding: shouldBePadding, centerText: shouldCenterText}">

css...

.blueBG{background-color:#B5EFF2;}
.tenPadding{padding:10px;}
.centerText{text-align:center;}

angular model

{
$scope.shouldBeBlue = true;
$scope.shouldBePadding = true;
$scope.shouldCenterText = true;
}
Sign up to request clarification or add additional context in comments.

1 Comment

impossible to do that. what if there are 100 x css styles and values?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.