1

我正在尝试使用 Karma 为 AngularJS 指令编写单元测试。为了能够在测试中使用指令模板,我使用了karma-ng-html2js-preprocessor

对于以下模板 HTML,我在单元测试中收到 Lexer 错误消息,但在实际系统中一切正常。

<div class="test"
    ng-style="{width: vm.width,
    height: vm.height,
    'margin-left': vm.x,
    'margin-top': vm.y}">
</div>

错误信息:

错误:[$parse:lexerr] Lexer Error: Unexpected next character at column 17-17 [] in expression [{width: vm.width,\n' + ' height: vm.height,\n' + ' \'margin -left\': vm.x,\n' + '\'margin-top\': vm.y}]。

这是预处理器中的错误还是我的表达有问题?

4

1 回答 1

1

我遇到了类似的问题,但我没有找到解决方法。尽管如此,我还是找到了避免它的方法。

我认为问题出在使用单引号 ( ') 上,当ng-html2js-preprocessor处理模板时,使用\'.

ng-style因此,在您的情况下,您需要避免在值中使用引号。即通过在控制器或其他地方为它定义一个范围:

$scope.mystyle = {
    width: vm.width,
    height: vm.height,
    'margin-left': vm.x,
    'margin-top': vm.y
}

然后在 tour 属性中使用它:

<div class="test" ng-style="mystyle"></div>

希望这可以帮助!

于 2016-05-08T18:07:32.570 回答