0

I would like to display styles which depend on dynamic values conditionally. For example:

<div ng-style="!obj.prop ? 'width : 0;' : 'width: {{ obj.val1 / obj.val2 }}%;'"></div>

Is this possible to do using ng-class or ng-style?

2

1 Answer 1

1

Use this refactored version and you would be fine:

<div ng-style="{ 'width' : !obj.prop ? 0 : obj.val1 / obj.val2 + '%' }"></div>

Your problem was that you were using interpolation ({{ and }}) inside a directive which requires an expression.

As a good rule of thumb, remember that most built-in AngularJS directives require an expression and not interpolation. A few directives which work with interpolation: ngHref, ngSrc.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.