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?
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?
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.