gpt4 book ai didi

angularjs-directive - 使用 Jasmine 和范围的测试指令通过引用传递

转载 作者:行者123 更新时间:2023-12-04 08:13:47 25 4
gpt4 key购买 nike

我正在尝试用 jasmine 测试一个指令。指令:

angular.module('app').directive('test',
function(){
return{
restrict: 'A',
scope:{data:'='},
link:function($scope,element,attrs){
.....
$scope.data=[200,300];
}
}
}
]);

Jasmine :

describe('test', function(){
beforeEach(module('test'));
beforeEach(inject(function($rootScope,$compile){
scope = $rootScope.$new();
element = '<div test data="{{data}}"></div>';
scope.data = [100,200]
element = $compile(element)(scope);
scope.$digest();
}));
it('is a test',function(){
expect(data).toBe([100,200]);
});
}

并且由于作用域使用“=”,它传递了引用。然而,当运行测试时,出现语法错误: token “数据”是意外的,期待[:]在表达式 [{{data}}] 的第 3 列。如果我只用 <test data={{data}}></test> 去掉测试模板中的 div ,它工作正常。当我在范围内用“@”替换“=”时,它也能正常工作。谁能给我一些关于如何将引用传递给范围的建议?谢谢。

最佳答案

经过两天的努力,我终于做到了。

describe('test', function(){
beforeEach(module('test'));
beforeEach(inject(function($rootScope,$compile){
$scope = $rootScope.$new();
element = '<div test data="databind"></div>';
$scope.databind = [100,200]
element = $compile(element)($scope);
$scope.$digest();
}));
it('is a test',function(){
tests.......
});

似乎 {{}} 在这里不起作用。通过使用范围,我们可以通过引用传递它。

关于angularjs-directive - 使用 Jasmine 和范围的测试指令通过引用传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27790448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com