我正在尝试将函数绑定到指令。这是我的小提琴代码:
var myApp = angular.module('myApp', []);
myApp.directive("myFoo", function () {
return {
restrict: "E",
scope: {
callback: '&'
},
template: '<span ng-click="callback()">MyFoo</span>'
};
});
myApp.directive("myBar", function () {
return {
restrict: "E",
template: '<div>MyBar > <my-foo test="cb"></my-foo></div>',
link: function (scope) {
scope.cb = function (x) {console.log('click');}
}
};
});
我必须做一些愚蠢的事情,因为当我点击my-foo
元素时回调不起作用。有什么建议为什么这个例子不起作用?
答案 0 :(得分:1)
您需要将其传递给您正在使用的回调属性,并将其直接与&#39; =&#39;:
相关联var myApp = angular.module('myApp', []);
myApp.directive("myFoo", function () {
return {
restrict: "E",
scope: {
callback: '='
},
template: '<span ng-click="callback()">MyFoo</span>'
};
});
myApp.directive("myBar", function () {
return {
restrict: "E",
template: '<div>MyBar > <my-foo callback="cb"></my-foo></div>',
link: function (scope) {
scope.cb = function (x) {console.log('click');}
}
};
});
jsFiddle:http://jsfiddle.net/xohv1syq/
jsFiddle使用&amp ;: http://jsfiddle.net/hzxcptzv/