Javascript 单元测试Karma Jasmine SyntaxError:&&引用;角度定向绑定
我在我的指令行中得到了Javascript 单元测试Karma Jasmine SyntaxError:&&引用;角度定向绑定,javascript,angularjs,angularjs-directive,karma-jasmine,Javascript,Angularjs,Angularjs Directive,Karma Jasmine,我在我的指令行中得到了SyntaxError:Parse error,我想使用来自父指令方法的“&”单向绑定 myApp.directive('datasourceDeleteBtn', [function() { return { restrict: 'E', replace: true, template: '<a href="#">✕</a>', scope: { datasourceIndex:
SyntaxError:Parse error
,我想使用来自父指令方法的“&”单向绑定
myApp.directive('datasourceDeleteBtn', [function() {
return {
restrict: 'E',
replace: true,
template: '<a href="#">✕</a>',
scope: {
datasourceIndex: '@',
removeParentDiv: '&'
},
link: link
};
function link(scope, element, attr) {
element.bind('click', function(event) {
event.preventDefault();
scope.deleteDatasource(scope.datasourceIndex);
});
// Notify parent directive
scope.deleteDatasource = function(datasource_index) {
// conditional stuff that happens not included
// {} required for passing values to "&" parent scope method
scope.removeParentDiv({datasource_index});
};
}
}]);
问题似乎是茉莉不喜欢{}。删除括号将导致测试通过(由于&requires{}而出现典型错误)
由于在中以错误格式传递json,因此出现错误
方法
您尚未从指令中正确调用在指令作用域removeParentDiv:'&'
中传递的方法。因为您只做了scope.removeParentDiv({datasource\u index})
不会将索引参数传递给方法
为了让它工作,你需要做一些改变
remove-parent-div="removeParentDiv(index)"
index
只是参数,datasource\u index
是它的值
scope.removeParentDiv({index: datasource_index});
scope.deleteDatasource(scope.datasourceIndex)后运行摘要循环从单击事件中的code>方法,以便更新范围
绑定
element.bind('click', function(event) {
event.preventDefault();
scope.deleteDatasource(scope.datasourceIndex);
scope.$apply(); //to run digest cycle, to keep binding in sync
});
谢谢我很好奇为什么需要在“&”单向绑定中传递对象{index:}。当我双向使用“=”时,如果没有这个对象语法,它可以正常工作。看起来my parentDiv函数需要更多的代码才能理解它何时得到{}而不是普通的索引字符串。@liquified在传递
&
方法的方法参数时,您需要遵循json结构。是的,当您有@
(单向绑定)时,我们将值从其属性传递给指令,当您从指令的外部上下文中更改它时,该值将在指令中更新,但从指令中更新该值不会(更新父项中的值)该值已更改。这就是为什么它被称为单向绑定。这就是代码不使用@
的原因,但当您使用=
双向绑定时,它会更新两侧的值,所以这就是它在该实例中工作的原因
scope.removeParentDiv({index: datasource_index});
element.bind('click', function(event) {
event.preventDefault();
scope.deleteDatasource(scope.datasourceIndex);
scope.$apply(); //to run digest cycle, to keep binding in sync
});