Angularjs 在Angular 1.4中,通过独立作用域的嵌套指令绑定函数失败
在嵌套指令中绑定函数时,1.4更新似乎引入了一个问题。我举了一个例子: 代码:Angularjs 在Angular 1.4中,通过独立作用域的嵌套指令绑定函数失败,angularjs,angular-directive,Angularjs,Angular Directive,在嵌套指令中绑定函数时,1.4更新似乎引入了一个问题。我举了一个例子: 代码: var-app=angular.module('myApp',[]); app.controller('myCtrl',function(){ this.searchFunction=函数(术语){ log('您搜索了'+term'); } }); app.directive('outerDirective',function(){ 返回{ 限制:'E', 范围:{ 外部:“&” }, 模板:“”, 控制器:函数()
var-app=angular.module('myApp',[]);
app.controller('myCtrl',function(){
this.searchFunction=函数(术语){
log('您搜索了'+term');
}
});
app.directive('outerDirective',function(){
返回{
限制:'E',
范围:{
外部:“&”
},
模板:“”,
控制器:函数(){
},
控制器:“cx”,
bindToController:true
};
});
app.directive('innerDirective',function(){
返回{
限制:'E',
范围:{
内部:“&”
},
模板:'),:2:54)
在目标位置。(匿名函数)[作为内部]()
在执行时()
fn时(评估时间(),:2:237)
在回调时()
在范围内。$eval()
在范围内。$apply()
在兰开夏
在HtmlanchoreElement.eventHandler()上
看起来您遇到了一个错误,因为您没有使用{param:value}
方法来调用指令中的函数。plunkr也缺少cx.outer
函数,所以我不确定我们希望看到的结果
我已经更新了您的plunkr,以演示它在Angular 1.4中使用显式参数传递:。谢谢@spikeheap。传递此对象表示法比正常调用函数更有效的原因是什么?
var app = angular.module('myApp', []);
app.controller('myCtrl', function(){
this.searchFunction = function(term) {
console.log('you searched for ' + term);
}
});
app.directive('outerDirective', function(){
return {
restrict: 'E',
scope: {
outer: '&'
},
template: '<inner-directive inner="cx.outer(term)"></inner-directive>',
controller: function(){
},
controllerAs: 'cx',
bindToController: true
};
});
app.directive('innerDirective', function(){
return {
restrict: 'E',
scope: {
inner: '&'
},
template: '<a ng-click="cx.execute()">Click</a>',
controller: function(){
this.execute = function(){
this.inner('fred');
}
},
controllerAs: 'cx',
bindToController: true
};
});