How to use a $Scope variable within ng-class

Asked

Viewed 233 times

1

I have the following code:

<a ng-class="{'text-{{color}}-6': menu == '#background'}" href="#background">
   My Menu
</a:

It works, at first it adds the text-red-6 class to the element. But when I change the color of the $scope, the ng-class does not change the class.

2 answers

2

I managed using ternary operator in the class attribute :

class="{{menu == '#background' ? 'text-' + color + '-6' : ''}}"

0

Try it this way:

<div ng-class="{'{{class}}': menu == '#background'}">

...

$scope.color = 'red';
$scope.class = 'text-' + color + '-6';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.