如果元素未定义,则应用一个类-AngularJS

Apply a class if element undefined - AngularJS

本文关键字:一个 -AngularJS 未定义 元素 应用 如果      更新时间:2023-09-26

只有当对象中的元素未定义/为null时,我才想将类应用于元素。我正在尝试下面的方法,但没有成功。。。

<li ng-repeat="post in posts.records | startFrom:currentPage*pageSize | limitTo:pageSize" ng-class="post.record.time !='' ? 'not-available' : '' ">
    <a href="record.php?id={{post.record.ID}}">
       <b>{{post.record.name}}</b>
    </a>
</li>

谢谢你的帮助。

<li ng-repeat="post in posts.records | startFrom:currentPage*pageSize | limitTo:pageSize" ng-class="{'not-available': post.record.time !=''}">
    <a href="record.php?id={{post.record.ID}}">
       <b>{{post.record.name}}</b>
    </a>
</li>

HTML

<li ng-repeat="post in posts.records | startFrom:currentPage*pageSize | limitTo:pageSize" ng-class="getClass(post)">
    <a href="record.php?id={{post.record.ID}}">
       <b>{{post.record.name}}</b>
    </a>
</li>

JS-

$scope.getClass=function(post){
  if(_isUndefinedOrNull(post.record.time))
     return 'not-available';
  else
     '';
}
function _isUndefinedOrNull(val) {
   return angular.isUndefined(val) || val === null || val === '';
}//