ChaseLouisPowers
ChaseLouisPowers

Reputation: 755

AngularJS ng-class if element exists

I'm trying to add a class to li elements if something inside the li exists. Here's my code:

<li ng-repeat="answer in answers" ng-class="{'textleft': textleft}">
    <p class="left" ng-bind="maintext"></p> 
    <p class="right" ng-bind="optionaltext" ng-model="textleft" ng-init="textleft=true" ng-if="answer.category != null"></p>
</li>

I'm trying to get the li to center text if only one of the p elements show up. It works if I declare ng-model textleft before the li ng-class. Anyone know how I can get it to work like this?

Upvotes: 0

Views: 1247

Answers (1)

Matt Way
Matt Way

Reputation: 33171

Why not use the same statement that you are using to determine whether or not to display the <p> tag, to set the class?

<li ng-repeat="answer in answers" ng-class="{'textleft': answer.category != null}">
    <p class="left" ng-bind="maintext"></p> 
    <p class="right" ng-bind="optionaltext" ng-model="textleft" ng-if="answer.category != null"></p>
</li>

Upvotes: 1

Related Questions