孤立的范围指令导致业力错误

时间:2016-01-15 11:03:46

标签: javascript angularjs karma-runner

我有一个带隔离范围的指令,我在其中传递一个绑定属性
"="

.directive('drawCircle',function (){
  return {
    restrict:'A',
    replace:false,
    scope:{
      point:'=',
    },
    template:' <circle '+
    'ng-attr-cx="{{point[0]}}" ng-attr-cy="{{point[1]}}" '+
    'r="3" fill="purple" />'
  };
 });

我用它:

 <g draw-circle point="[22,33]">

它在浏览器中工作正常,但是karma抛出:

Error: [$compile:multidir] Multiple directives [drawCircle (module: myApp), drawCircle (module: myApp)]
asking for new/isolated scope on: <g draw-circle="" point="p">

这是创建错误的单元测试:

 describe('directives testing', function() {
 var element, scope, compiled;

beforeEach(module('draw.path'));

  describe('draw-single-point directive',function(){
          beforeEach(inject(function($rootScope,$compile){

              scope = $rootScope.$new();
              element = angular.element('<g draw-circle point="p" ></g>');
              compiled = $compile(element);
              scope.p=[110,11];
              compiled(scope);

              scope.$digest();
          }));


  });

为什么Karma抱怨?

1 个答案:

答案 0 :(得分:1)

这是您面临的可能问题

您的指令drawCircle已定义/加载两次,如果发生这种情况,您将获得确切的错误 - 请检查该笔:http://codepen.io/maurycyg/pen/pgWyEy?editors=101

我没有看到您的测试代码或指令有任何问题,甚至没有将指令与ng-repeat一起使用,所以我建议检查KARMA配置,也许你加载指令两次