2

我正在尝试测试以下指令。

angular.module('myModule.directives', [])
  .directive('test', function() {
    return {
      restrictions: 'E',
      scope: {},
      transclude: true,
      replace: true,
      template: '<div><ui><li>a</li></ul></div>'
    }
});

在我的测试中:

describe('directives', function () {
    var compile,
    scope,
    test;

    beforeEach(module('myModule.directives'));
    beforeEach(inject(function ($compile, $rootScope) {

    compile = $compile;
    scope = $rootScope;
    test = angular.element('<test></test>');
    compile(test)(scope);
    scope.$digest();

    it('should render the test directive', function () {
        var lis = test.find('li');
        console.log(lis.length); // should output 1
    });

}));  

测试失败并输出 0。我很好奇 compile 是否真的在做任何事情。如果我将测试替换为

  test = angular.element("<test><li></li></test>");. 

然后“查找”确实在测试元素中找到了 li,这表明编译没有渲染到

  '<div><ui><li>a</li></ul></div>' 

正如它应该。有任何想法吗?我查看了以下视频以获取指导(http://www.youtube.com/watch?v=rB5b67Cg6bc)。

4

1 回答 1

2

您需要将“限制”更改为“限制”

于 2013-04-18T16:10:41.107 回答