无法读取属性'insertBefore'在forEach.after处为null

Cannot read property 'insertBefore' of null at forEach.after

本文关键字:forEach after 处为 null insertBefore 读取 属性      更新时间:2023-09-26

我有一个非常简单的html页面,在那里我使用ng-include:-加载两个不同的html文件

<body ng-controller="testCtrl">
    <div class="panel panel-primary">
        <h3 class="panel-heading">Test</h3>
        <ng-include src="'View/Details.html'" ng-show="details" />
        <ng-include src="'View/Summary.html'" ng-show="summary" />
    </div>
</body>

最初只有details变量为真,因此它正在加载Details视图,该视图如下所示:-

<div class="panel-body">
    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="item in items">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
            </tr>
        </tbody>
    </table>
</div>

这是控制器供参考:-

.controller("testCtrl", function ($scope) {
     $scope.loadItems = function () {
          $scope.items= [
              { id: 1, name: "dummy" },
              { id: 2, name: "dummy1" },
              { id: 3, name: "dummy2" }
            ];
          }
        $scope.loadItems();
      }

当我加载页面时,它正确显示数据,但没有触发任何事件(所有按钮点击处理程序都通过ng-click&与控制器中定义的行为关联),所以我继续检查控制台,它显示以下错误:-

TypeError:无法读取null的属性"insertBefore"位于forEach.after(myUrl)在Object.JQLite.(匿名函数)[在之后]

我对Angular很陌生。我看到了一些线程,并试图操纵我的标记代码,但没有成功。

您不能<ng-include/>--应该是<ng-include></ng-include>。同样,ng-if通常比ng-show更好,因为它减少了结果html的数量。

休息工作:plnkr.co/edit/EmUYuHltWaXnjEC6juid?p=预览。