如何使用ng-if和ng-repeat检查空值?

时间:2017-08-08 13:00:56

标签: angularjs

我在此代码中有错误我尝试使用ng-if检查空值如果使用重复

<div class="col-md-4 col-sm-6" ng-repeat="player in  PlayerData">
     <div class="club-sport">
          <a href="../../Player/PlayerProfile/{{player['ID']}}" target="_blank">
               <img src="../../images/Players/{{player['ProfileImage']}}" class="player-img">
               <h4> {{player['Name']}}   </h4>
                <div class="border-bottom"></div>
                <p> {{player['ClubName']}}  </p>
           </a>
     </div>
     <div class="no-result" ng-if="!player">
           <p> لا يوجد لاعبين </p>
     </div>
</div>

3 个答案:

答案 0 :(得分:0)

如果ng-if="player['Name']"属性为null,则只使用Name,这将返回false

<div class="col-md-4 col-sm-6" ng-repeat="player in  PlayerData">
    <div class="club-sport">
       <a href="../../Player/PlayerProfile/{{player['ID']}}" target="_blank">
          <img src="../../images/Players/{{player['ProfileImage']}}" class="player-img">
          <h4  ng-if="player['ClubName']" > {{player['Name']}} </h4>
          <div class="border-bottom"></div>
          <p ng-if="player['ClubName']"> {{player['ClubName']}} </p>
       </a>
   </div>
   <div class="no-result" ng-if="!player">
       <p> لا يوجد لاعبين </p>
   </div>
</div>

答案 1 :(得分:0)

首先感谢您的关注 我尝试更新代码并在ngrepeat中制作

لايوجدلاعبين

但是消息“لايوجدلاعبين”在我看来两个案例中显示错误

<div class="col-md-4 col-sm-6" ng-repeat="player in  PlayerData">
                <div class="club-sport" ng-if="player.Name">
                    <a href="../../Player/PlayerProfile/{{player['ID']}}" target="_blank">
                        <img src="../../images/Players/{{player['ProfileImage']}}" class="player-img">
                        <h4> {{player['Name']}}   </h4>
                        <div class="border-bottom"></div>
                        <p> {{player['ClubName']}}  </p>
                    </a>
                </div>
            </div>
            <div class="no-result" ng-if="!player.Name">
                <p> لا يوجد لاعبين </p>
            </div>

答案 2 :(得分:0)

由于玩家是一个对象,即使它没有属性也会存在。更好的方法是检查是否存在特定属性。在这种情况下,我正在检查'Name'属性是否存在。

<div class="col-md-4 col-sm-6" ng-repeat="player in  PlayerData">
     <div class="club-sport">
          <a href="../../Player/PlayerProfile/{{player['ID']}}" target="_blank">
               <img src="../../images/Players/{{player['ProfileImage']}}" class="player-img">
               <h4> {{player['Name']}}   </h4>
                <div class="border-bottom"></div>
                <p> {{player['ClubName']}}  </p>
           </a>
     </div>
     <div class="no-result" ng-if="!player['Name']">
           <p> لا يوجد لاعبين </p>
     </div>
</div>