简体   繁体   English

AngularJs在指令中使用ng-repeat给出类型错误

[英]AngularJs using ng-repeat in a directive gives type error

I've just started using angularjs. 我刚刚开始使用angularjs。 I have created a controller and a directive. 我创建了一个控制器和一个指令。 The directive is to create a tabs content, the controller manages the tabs' contents. 该指令是创建选项卡内容,控制器管理选项卡的内容。

the controller: 控制器:

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

the directive: 指令:

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');
      }
    }]
  }
});

the directive html code: 指令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>

I get this error and I have no idea why: 我收到此错误,我也不知道为什么:

TypeError: Cannot read property 'childNodes' of undefined TypeError:无法读取未定义的属性“ childNodes”

When i remove the ng-repeat lines then error dissappears. 当我删除ng-repeat行时,错误消失了。

Any suggestion is appriciated 任何建议都适用

The controller of your directive is not correct controller: function($scope, $element, $attrs, $transclude, otherInjectables) { ... } 您的指令的控制器不是正确的controller: function($scope, $element, $attrs, $transclude, otherInjectables) { ... }

$sce should come after attrs and transclude. $ sce应该位于attrs之后并包含在内。 You need to include both even if you are not using it 即使您不使用它,也需要同时包含两者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM