html - AngularJS ngClass conditional -


is there way make expression ngclass conditional. example, have tried following:

<span ng-class="{test: 'obj.value1 == \'someothervalue\''}">test</span> 

the issue code no matter obj.value1 is, class test applied element. doing this:

<span ng-class="{test: obj.value2}">test</span> 

as long obj.value2 not equal truthy value, class in not applied. can work around issue in first example doing this:

<span ng-class="{test: checkvalue1()}">test</span> 

where checkvalue1 function looks this:

$scope.checkvalue1 = function() {   return $scope.obj.value === 'somevalue'; } 

i wondering if how ngclass supposed work. building custom directive similar this. can't find way watch expression (and maybe impossible , reason why works this).

here plnkr show mean:

http://plnkr.co/edit/ish0t8swdeygbh7ylzg2?p=preview

your first attempt right, should work without quotes.

{test: obj.value1 == 'someothervalue'} 

here plnkr.

the ngclass directive work expression evaluates truthy or falsey, bit similar javascript expressions differences, can read here. if conditional complex, can use function returns truthy or falsey, did in third attempt.

just complement: can use logical operators form logical expressions

ng-class="{'test': obj.value1 == 'someothervalue' || obj.value2 == 'somethingelse'}" 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -