Jasmine单元测试:$ compile从ng-repeat中产生注释

时间:2013-05-24 12:25:44

标签: unit-testing angularjs comments jasmine angularjs-ng-repeat

为什么行

var scope = $rootScope.$new();
dump($compile('<ul><li ng-repeat="item in [1,3,5,7,9]">{{item}}</li></ul>')(scope));

结果:

'<ul class="ng-scope"><!-- ngRepeat: item in [1,3,5,7,9] --></ul>'

我希望看到<li>件而非评论。

1 个答案:

答案 0 :(得分:16)

你错过了digest方法,一路走

var e = $compile('<div><ul><li ng-repeat="item in [1,3,5,7,9]">{{item}}</li></ul></div>')(scope);
scope.$digest();
console.log(e.html());

这是我现在得到的输出:

Safari 537.21 (Linux) LOG: '
<ul><!-- ngRepeat: item in [1,3,5,7,9] -->
<li ng-repeat="item in [1,3,5,7,9]" class="ng-scope ng-binding">1</li>
<li ng-repeat="item in [1,3,5,7,9]" class="ng-scope ng-binding">3</li>
<li ng-repeat="item in [1,3,5,7,9]" class="ng-scope ng-binding">5</li>
<li ng-repeat="item in [1,3,5,7,9]" class="ng-scope ng-binding">7</li>
<li ng-repeat="item in [1,3,5,7,9]" class="ng-scope ng-binding">9</li>
</ul>'