ng-repeat 忽略立即 ng-if

ng-repeat ignores immediate ng-if

我已经使用 Angular JS 很长时间了,但现在遇到了一个奇怪的问题,即 ng-ifng-repeat.

忽略了

这是示例和简单代码,没有按预期工作

<ul ng-repeat="detail in details">
  <span ng-if="2 == 3"> <!-- This should block the next tags to execute-->
    <li>{{detail.name}}
      <ul ng-repeat="detailed in details.name">
        <li>{{detailed.prof}}</li>
      </ul>
    </li>
  </span>
  <span ng-if="2 === 2"> <!-- Instead this should get print -->
    Print this (Updated)
  </span>
</ul>

这是我的 plunkr : https://plnkr.co/edit/xy4Qyd4tXm6kWROiaFVR?p=preview

使用这个版本

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

或使用ng-if -> ng-show

你要找的是这个:

 <div ng-show="'2' == '3'">
    <li>{{detail.name}}
       <ul ng-repeat="detailed in details.name">
         <li>{{detailed.prof}}</li>
        </ul>
     </li>
 </div>

而不是使用 ng-if 使用 ng-show.

I have update you punker

请看this

问题是版本问题。您也可以将其转换为数字 (ng-if="+2 === +2") 尝试使用此代码代替您的

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
  <ul ng-repeat="detail in details">
    <span ng-if="+2 === +3">
      <li>{{detail.name}}
        <ul ng-repeat="detailed in details.name">
          <li>{{detailed.prof}}</li>
        </ul>
      </li>
    </span>
    <span ng-if="+2 === +2">
      Nothing printed
    </span>
  </ul>

  </body>

</html>