0

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?

3
  • Can you share the rest of your code? Commented Nov 2, 2015 at 23:47
  • Do you want add 'textleft' class to your li if the second p tag is showing? Commented Nov 2, 2015 at 23:47
  • Matt Way helped me and fixed my broken code Commented Nov 3, 2015 at 1:43

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.