简体   繁体   English

AngularJS ng-class如果元素存在

[英]AngularJS ng-class if element exists

I'm trying to add a class to li elements if something inside the li exists. 如果li内部存在某种东西,我想为li元素添加一个类。 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. 如果只显示p元素之一,我试图使li居中。 It works if I declare ng-model textleft before the li ng-class. 如果我在li ng-class之前声明ng-model textleft,它会起作用。 Anyone know how I can get it to work like this? 有人知道我如何使它像这样工作吗?

Why not use the same statement that you are using to determine whether or not to display the <p> tag, to set the class? 为什么不使用与用于确定是否显示<p>标记来设置类的语句相同的语句?

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM