Gab*_*iel 3 pass-by-reference jasmine angularjs-directive
我正试图用茉莉花测试一个指令.指示:
angular.module('app').directive('test',
function(){
return{
restrict: 'A',
scope:{data:'='},
link:function($scope,element,attrs){
.....
$scope.data=[200,300];
}
}
}
]);
Run Code Online (Sandbox Code Playgroud)
茉莉:
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]);
});
}
Run Code Online (Sandbox Code Playgroud)
并且由于范围使用"=",它传递引用.运行测试然而,当有很语法错误:令牌"数据"是出乎意料的,希望[:]在表达式3栏[{{数据}}].如果我只是在测试模板中删除div <test data={{data}}></test>
,它工作正常.当我在范围内用"@"替换"="时它也可以正常工作.谁能给我一些关于如何将引用传递给范围的建议?谢谢.
经过两天的尝试,我终于可以做到了.
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.......
});
Run Code Online (Sandbox Code Playgroud)
似乎{{}}在这里不起作用.通过使用范围,我们可以通过引用传递它.