【问题标题】:Cannot read property 'insertBefore' of null at forEach.after无法在 forEach.after 处读取 null 的属性“insertBefore”
【发布时间】:2015-12-04 13:33:05
【问题描述】:

我有一个非常简单的 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.(匿名函数) [as after]

我对 Angular 还是很陌生。我看到很少的线程并试图操纵我的标记代码但没有运气。

【问题讨论】:

  • 有错别字请使用{ id: 2, name: "dummy1" }, 而不是{ id: 2, name: "dummy1 },
  • 他说你在 dummy1 之后漏掉了一个 ( " )
  • 请在 dummy1 之后添加双引号 ",就像我说的那样
  • @Vineet - 好的,谢谢,但这不是我的代码中的问题,这是一个错字。
  • 首先,你的html是无效的。你不能——应该是。 ng-if 通常也比 ng-show 好,因为它减少了结果 html 的数量。休息工作:plnkr.co/edit/EmUYuHltWaXnjEC6juid?p=preview。我知道你在哪里得到那个错误。

标签: javascript html angularjs


【解决方案1】:

你不能&lt;ng-include/&gt;——应该是&lt;ng-include&gt;&lt;/ng-include&gt;。 ng-if 通常也比 ng-show 好,因为它减少了结果 html 的数量。

其余作品:plnkr.co/edit/EmUYuHltWaXnjEC6juid?p=preview.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    相关资源
    最近更新 更多