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:
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
Post a Comment