我通過傳遞函數的指令有問題(熟悉這個帖子:AngularJS - pass function to directive但我不能得到它的工作)AngularJS:傳遞函數指令
這裏是我的代碼:
指令:
.directive('testdirective', function(){
return{
restrict: 'E',
scope: {
onClick: '&'
},
controller: 'TestController',
controllerAs: 'tc',
bindToController: true,
template: '<div><button ng-click="onClick()">What UP</button></div>',
replace: true
}
})
控制器:
TestController.$inject = ["$scope"];
function TestController($scope) {
$scope.testFunction = function(){
alert("I´m the alert of the TestContoller");
};
$scope.test = 'test';
}
HTML:
<div>
<testdirective on-click="testFunction()"></testdirective>
</div>
我想要的聲音非常簡單,我只想將該函數傳遞給指令並在ng按鈕上執行該按鈕。
對我來說,我的代碼看起來完全like this fiddle 但我的不工作:/
將是真棒,如果有人有一些提示我。
編輯
我的指令都需要自己的控制!
後來要傳入的函數將來自另一個控制器!
什麼是你的錯誤? –
什麼也沒有 - 只是沒有執行 –
工作的JSFiddle擁有控制器**外**指令。你的代碼在**指令裏面有控制器**。您的指令使用**隔離範圍**。範圍**外**的函數不會被範圍內** **繼承。 – georgeawg