我有一个angular.js指令如下,指令的onChange
属性使用了&
绑定策略:
// 指令定义
function TestDirective(){
return {
template:"<div>hello</div>",
scope:{
"onChange":"&",
},
link:function(){ onChange() } // 执行传入的回调函数
}
}
使用指令时,当我将回调函数放到Scope上时,是可以成功调到的:
<test on-change="doSomething()"></test>
但是当我将回调函数放在Controller上时,却调不到:
<test on-change="$ctrl.doSomething()"></test>
这是为什么?
angularjs 官方文档有讲到&绑定策略,但是没有找到为什么"将回调函数放在controller上调用不成功"的原因
框架这样规定的,你定义TestDirective时不也是吧onChange做为scope的属性,所以“&绑定”时都是通过scope的。