ng-如果不使用 ng-repeat

ng-if not working with ng-repeat

这应该是一个简单的问题,但我不明白发生了什么。 基本上,我使用 ng-if 和 ng-repeat 将对象传递给控制器​​中的函数调用。 但是我的函数没有看到传递给它的参数。它说未定义。

var app = angular.module('newformController', []);

app.controller('NewFormController', ['$scope', '$log',
            function($scope, $log) {
              
     $scope.meta = {
      "GRANT": {
        "VALUE": "2",
        "DEFAULT": "1",
        "RANGE": "1:4",
        "TYPE": "INTEGER",
        "DESCRIPTION": "Some description"
      }
    };

    $scope.testing = function(x) {
        $log.debug('X is: ' + x);
        if (x == "1:4") {
          return true;
        } else {
          return false;
        }
    };
    
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<tr ng-repeat="(param,value) in meta">
  <td>{{param}}</td>
  <td>{{value.DESCRIPTION}}</td>
  <span ng-if="testing(value.RANGE)">
                  <td><input type="text" class="form-control" value="{{value.DEFAULT}}"></td>
              </span>
</tr>

为什么控制台打印 'undefine' ?为什么这个函数会被调用这么多次?我只有一把钥匙,ng-repeat不应该只循环一次吗?

现在,如果我而不是 "value.RANGE" 将一些字符串传递给函数,则该字符串将 read/printed 正确...

如果您使用有效 html,它会正常工作

<tr ng-repeat="(param,value) in meta">
  <td>{{param}}</td>
  <td>{{value.DESCRIPTION}}</td>

  <td ng-if="testing(value.RANGE)">
    <input type="text" class="form-control" value="{{value.DEFAULT}}">
  </td>

</tr>

<span> 不是 <tr> 的有效子项,因此浏览器可能会将其移出 table,因此移出 ng-repeat 子范围。然后当 angular 运行该函数时,它在不同的范围内

working version

这是因为你有无效的 html - 看看这个例子 - http://jsfiddle.net/HB7LU/16355/

我刚刚将您的 table 代码修改为如下所示

<table>
    <tr ng-repeat="(param,value) in meta">
        <td>{{param}}</td>
        <td>{{value.DESCRIPTION}}</td>
        <td ng-if="testing(value.RANGE)">
            <input type="text" class="form-control" value="{{value.DEFAULT}}">
        </td>
    </tr>
</table>

而且有效:)。