在 Angular 1.4 中通过隔离范围的嵌套指令绑定函数失败
Binding functions through nested directives with isolated scope failing in Angular 1.4
1.4 更新似乎在嵌套指令中绑定函数时引入了一个问题。我有一个例子 plunker:http://plnkr.co/edit/fQLY0N8BTNs38VC8ikll
代码:
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
};
});
这在 1.3 中有效,但我应该在 1.4 中使用一些新方法吗?
单击 link 您将在控制台中看到以下错误:
TypeError: Cannot use 'in' operator to search for 'cx' in fred
at fn (eval at (https://code.angularjs.org/1.4.0/angular.js:13036:15), :2:54)
at destination.(anonymous function) [as inner] (https://code.angularjs.org/1.4.0/angular.js:8689:22)
at execute (http://run.plnkr.co/zE9xlCQNMBrOZZgi/script.js:35:14)
at fn (eval at (https://code.angularjs.org/1.4.0/angular.js:13036:15), :2:237)
at callback (https://code.angularjs.org/1.4.0/angular.js:23090:17)
at Scope.$eval (https://code.angularjs.org/1.4.0/angular.js:15719:28)
at Scope.$apply (https://code.angularjs.org/1.4.0/angular.js:15818:23)
at HTMLAnchorElement. (https://code.angularjs.org/1.4.0/angular.js:23095:23)
at HTMLAnchorElement.eventHandler (https://code.angularjs.org/1.4.0/angular.js:3247:21)
您似乎遇到了错误,因为您没有使用 {param: value}
方法在指令中调用函数。 plunkr 也缺少 cx.outer
功能,所以我不确定我们希望看到什么。
我已经更新了你的 plunkr 以演示它与 Angular 1.4 一起工作并显式传递参数:http://plnkr.co/edit/T7aasD?p=preview.
1.4 更新似乎在嵌套指令中绑定函数时引入了一个问题。我有一个例子 plunker:http://plnkr.co/edit/fQLY0N8BTNs38VC8ikll
代码:
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
};
});
这在 1.3 中有效,但我应该在 1.4 中使用一些新方法吗?
单击 link 您将在控制台中看到以下错误:
TypeError: Cannot use 'in' operator to search for 'cx' in fred at fn (eval at (https://code.angularjs.org/1.4.0/angular.js:13036:15), :2:54) at destination.(anonymous function) [as inner] (https://code.angularjs.org/1.4.0/angular.js:8689:22) at execute (http://run.plnkr.co/zE9xlCQNMBrOZZgi/script.js:35:14) at fn (eval at (https://code.angularjs.org/1.4.0/angular.js:13036:15), :2:237) at callback (https://code.angularjs.org/1.4.0/angular.js:23090:17) at Scope.$eval (https://code.angularjs.org/1.4.0/angular.js:15719:28) at Scope.$apply (https://code.angularjs.org/1.4.0/angular.js:15818:23) at HTMLAnchorElement. (https://code.angularjs.org/1.4.0/angular.js:23095:23) at HTMLAnchorElement.eventHandler (https://code.angularjs.org/1.4.0/angular.js:3247:21)
您似乎遇到了错误,因为您没有使用 {param: value}
方法在指令中调用函数。 plunkr 也缺少 cx.outer
功能,所以我不确定我们希望看到什么。
我已经更新了你的 plunkr 以演示它与 Angular 1.4 一起工作并显式传递参数:http://plnkr.co/edit/T7aasD?p=preview.