javascript - AngularJs 在指令中使用 ng-repeat 会出现类型错误

标签 javascript jquery angularjs

我刚刚开始使用 angularjs。我创建了一个 Controller 和一个指令。该指令用于创建选项卡内容, Controller 管理选项卡的内容。

Controller :

myApp.controller('companySalesController', function ($scope) {
  $scope.sales = [
    {
      date:"August, 14th 2014",
      action:"Sold Best sports watches via Online Coupone",
      earning:"50$"
    }
  ]
});

指令:

myApp.directive("myTabs", function () {
  return {
    restrict: "E",
    transclude:false,
    scope:false,
    controller: ['$scope','$element','$sce','$compile', function ($scope, $element,$sce,$compile) {
      var titles = $('.titles li',$element).get();
      var separator = angular.element("<div class='separator'><ul class='tabs-highlight'></ul></div>");
      $('.titles',$element).after(separator);
      for(var i=0; i< titles.length;i++){
        var title = titles[i];
        title.setAttribute('ng-click','selectTab('+i+')');
        $('.tabs-highlight',separator).append("<li></li>");
      }
      $('.titles li').css({
        width:($('.titles').width()/titles.length)
      });
      $('.separator li').css({
        width:($('.titles').width()/titles.length)
      });

      $compile($element.contents())($scope);

      $scope.selectTab = function (index) {
        $('ul.titles li').removeClass('select');
        $('ul.titles li:nth-child('+(index+1)+')').addClass('select');
        $('ul.tabs-highlight li').removeClass('select');
        $('ul.tabs-highlight li:nth-child('+(index+1)+')').addClass('select');
        $('div.tab-contents > div').removeClass('select');
        $('div.tab-contents > div:nth-child('+(index+1)+')').addClass('select');
      }
    }]
  }
});

指令html代码:

<my-tabs class="my-tabs">
<ul class="titles">
    <li>Published Rewards</li>
    <li>Pending Rewards</li>
    <li>Sales Overview</li>
</ul>
<div class="tab-contents">
    <div>

    </div>
    <div>

    </div>
    <div>
        <table class="company-sales">
            <tr>
                <th>Date</th>
                <th>Action</th>
                <th>Earning</th>
            </tr>
            <tr ng-repeat="s in sales">
                <td>{{s.date}}</td>
                <td>{{s.action}}</td>
                <td>{{s.earning}}</td>
            </tr>
        </table>
    </div>
</div>

我收到此错误,但我不知道为什么:

TypeError: Cannot read property 'childNodes' of undefined

当我删除 ng-repeat 行时,错误就会消失。

任何建议都值得采纳

最佳答案

指令的 Controller 不正确controller: function($scope, $element, $attrs, $transclude, otherInjectables) { ... }

$sce 应该位于 attrs 和 transinclude 之后。即使您不使用它,也需要将两者都包含在内

关于javascript - AngularJs 在指令中使用 ng-repeat 会出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25949339/

相关文章:

javascript - c3 图表中的多线标签

javascript - polymer 变化自定义元素

javascript - $broadcast 在 ng Repeat 上被多次调用,即使从 Controller 调用一次

javascript - 无法减少数组中相关的 JavaScript 对象

javascript - 将 d3.csv 修改为小写列名的正确方法

javascript - 检查切换是否可见或隐藏,如果隐藏则删除类

javascript - POST 数据不包含任何表单数据

javascript - 如何复制一个 div 及其内容,并将其附加到其他地方但保持原始隐藏

angularjs - Angular-ui的工具提示无法在ng-grid中正确显示

javascript - Angular JS 使用 $resource 并等待 promise 得到解决